- 
      
- 
        Save INDIAN2020/a39d1667d64a88a845cb1b90d60d350c to your computer and use it in GitHub Desktop. 
    Create WP User
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | public function custom_create_user($user_name, $first_name, $last_name, $email_address, $user_role = 'subscriber') | |
| { | |
| $user_id = username_exists( $user_name ); | |
| if( $user_id != null ) | |
| { | |
| throw New Exception('Username already exists!'); | |
| } | |
| if ( email_exists($email_address) == true ) | |
| { | |
| throw new Exception('E-mail already registered'); | |
| } | |
| if ( $user_role == '' ) | |
| { | |
| throw new Exception('Don't create users without roles'); | |
| } | |
| // Generate the password and create the user | |
| $password = wp_generate_password( 12, false ); | |
| $user_id = wp_create_user( $user_name, $password, $email_address ); | |
| // Update USER info | |
| wp_update_user( | |
| array( | |
| 'ID' => $user_id, | |
| 'nickname' => $email_address, | |
| 'first_name' => $first_name, | |
| 'last_name' => $last_name | |
| ) | |
| ); | |
| // Set the role | |
| $user = new WP_User( $user_id ); | |
| $user->set_role( $user_role ); | |
| // If need to add meta data | |
| // Add user meta | |
| // add_user_meta( $user_id, 'META_DATA_KEY', 'META_KEY_VALUE'); | |
| // Optional: Return user_id | |
| // return array('user_id' => $user_id, 'user_name' => $user_name); | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment