Support » Fixing WordPress » How to add full name automatically

  • Resolved quinz

    (@quinz)


    Hello everyone,

    I’m trying to get the full name automatically added to an account I create. It’s part of my SSO plugin so the full name is already stored in variables (first name and second name are separated).

    It seems that wp_create_user() just takes username, password and email as parametres so I cannot use it straight. I also tried to use wp_insert_user to update the data to the account that was just created without full name but didn’t get it working. So any ideas what would be the most elegant way to do this? Thank you very much in advance.

    function _create_user($username) {
    			$password = $this->_get_password();
    			$email_domain = $this->get_option('http_authentication_auto_create_email_domain');
    			$useCompanyEmail = $this->get_option('http_authentication_auto_use_company_email');
    
    			require_once(WPINC . DIRECTORY_SEPARATOR . 'registration.php');
    			if($useCompanyEmail){
    				wp_create_user($username, $password, $_SERVER['HTTP_EMAIL']);
    			}else{
    				wp_create_user($username, $password, $username . ($email_domain ? '@' . $email_domain : ''));
    			}
    
    			//The following will add the full name to the just created account !!! THIS IS THE PART I COULDN'T GET WORKING !!!
    
    			$first_name = $_SERVER[HTTP_FIRSTNAME];
    			$last_name = $_SERVER[HTTP_LASTNAME];
    
    			$userdata_new = array();
    			$userdata_new['user_pass'] = $password;
    			$userdata_new['user_login'] = $username;
    			$userdata_new['first_name'] = $first_name;
    			$userdata_new['last_name'] = $last_name;
    
    			wp_insert_user($userdata_new);
    		}
Viewing 1 replies (of 1 total)
  • Ah I solved the problem myself. I had to go by the wp_create_user function and use wp_insert_user for everything, this is the final working code if someone is interested:

    function _create_user($username) {
    			$password = $this->_get_password();
    			$email_domain = $this->get_option('http_authentication_auto_create_email_domain');
    			$useCompanyEmail = $this->get_option('http_authentication_auto_use_company_email');
    
    			require_once(WPINC . DIRECTORY_SEPARATOR . 'registration.php');
    			if($useCompanyEmail){
    				$user_email = $_SERVER['HTTP_EMAIL'];
    			}else{
    				$user_email = $username . ($email_domain ? '@' . $email_domain : '');
    			}
    
    			global $wpdb;
    
    			$user_login = $wpdb->escape( $username );
    			$user_pass = $password;
    			$first_name = $_SERVER['HTTP_FIRSTNAME'];
    			$last_name = $_SERVER['HTTP_LASTNAME'];			
    
    			$userdata = compact('user_login', 'user_email', 'user_pass', 'first_name', 'last_name');
    			wp_insert_user($userdata);
    		}

    Afterall it’s nothing but a modified version of wp_create_user

Viewing 1 replies (of 1 total)
  • The topic ‘How to add full name automatically’ is closed to new replies.