phpwordpress

Adding extra fields to Wordpress user registration


The first function takes a user that's registering on Wordpress and using the 'registration_errors' filter inserts him into an external db. I also need the ID that's returned by the Salesforce system to be inserted into the Wordpress usermeta table. Initially, I tried using $user_id as a global from the main wp_insert_user function to do a update_user_meta( $user_id, 'SF_User_ID', $SFUserID ); But, the $user_id variable wasn't returning anything. Now, I'm trying to do the update_user_meta in a separate function, but it needs to use the value that's passed from the first function. The current code returns this error: The current code doesn't work because $error is not being returned.

I can't figure out how to get the Wordpress userID and update usermeta table with the ID that's being returned by Salesforce in the $SFUserID variable.

 function add_user_to_SF($errors, $sanitized_user_login, $user_email ) {

    if ( $errors->get_error_code() ) return $errors;

    try {
        $sObject = new stdclass();
        $sObject->FirstName = $_POST['user_login'];
        $sObject->LastName = $_POST['user_login'];
        $sObject->Email = $_POST['user_email'];

        $createResponse = $mySforceConnection->create(array($sObject), 'Contact');

        $ids = array();
            foreach ($createResponse as $createResult) {
                array_push($ids, $createResult->id);
                $SFUserID = $createResult->id;

                                    add_action('user_register', 'addSFUserID', 10, 1);
            }

            } catch (Exception $e) {

              $errors->add( 'demo_error', __('<strong>ERROR</strong>: There is a Salesforce problem.','mydomain') );
              return $errors;
        }

              return $errors;
 }

 add_filter( 'registration_errors', 'add_user_to_SF', 10, 3 );

 function addSFUserID($user_id) {   

    update_user_meta( $user_id, 'SF_User_ID', $SFUserID );
 }

Solution

  • to resolve this error: Call to a member function get_error_code() on a non-object

    currently you are checking $errors->get_error_code(), in the case that $errors is not a wp_error, get_error_code() method doesnot exist and it would fail.

    to check if its an error, try using:

    is_wp_error( $errors );
    

    http://codex.wordpress.org/Function_Reference/is_wp_error

    so your if statement would look something like this:

    if ( is_wp_error( $errors ) ) return $errors;
    

    this way if there are no errors on registration it would continue to your logic below. I didn't read the rest of your logic but that was the first thing I noticed about the function. let me know if it works.

    Re- the second part of the question:

    this could use a lot of tweaking but in response to your second part of the question, I think the best way is to use a global var. I havent tested this code, but it should look something like this:

    $SFUserID = '';
    
    function add_user_to_SF($errors, $sanitized_user_login, $user_email ) {
    
        global $SFUserID;
    
        if ( is_wp_error( $errors ) ) return $errors;
    
        try {
    
            $sObject = new stdclass();
            $sObject->FirstName = $_POST['user_login'];
            $sObject->LastName = $_POST['user_login'];
            $sObject->Email = $_POST['user_email'];
    
            $createResponse = $mySforceConnection->create(array($sObject), 'Contact');
    
            $ids = array();
            foreach ($createResponse as $createResult) {
                array_push($ids, $createResult->id);
                $SFUserID = $createResult->id;
            }
    
        } catch (Exception $e) {
    
              $errors->add( 'demo_error', __('<strong>ERROR</strong>: There is a Salesforce problem.','mydomain') );
              return $errors;
        }
    
              return $errors;
    
    }
    
    
    function addSFUserID( $user_id ) {
    
        global $SFUserID;
        if( !empty( $SFUserID ) )
            update_user_meta( $user_id, 'SF_User_ID', $SFUserID );
    
    }
    
    add_filter( 'registration_errors', 'add_user_to_SF', 10, 3 );
    add_action('user_register', 'addSFUserID', 10, 1);