Title: Programmatically Add User Using wp_insert_user()
Last modified: August 20, 2016

---

# Programmatically Add User Using wp_insert_user()

 *  [EL45](https://wordpress.org/support/users/el45/)
 * (@el45)
 * [14 years, 10 months ago](https://wordpress.org/support/topic/programmatically-add-user-using-wp_insert_user/)
 * I am trying to add a user to WordPress programmatically using information contained
   in a session from our larger site.
 * [http://pastebin.com/0akbti27](http://pastebin.com/0akbti27)
 * I have verified that `wp_insert_user` is being called, no error is thrown, and
   the expected ID is returned. However, no user is added to the database.
 * I have also tried the simpler `wp_create_user` however, it always returns 0.
 * Someone mentioned adding `include_once(ABSPATH . 'wp-admin/includes/admin.php');`
   That did not resolve the issue. Does anyone have any insight why the user is 
   not being created?

Viewing 8 replies - 1 through 8 (of 8 total)

 *  [onelife1985](https://wordpress.org/support/users/onelife1985/)
 * (@onelife1985)
 * [14 years, 10 months ago](https://wordpress.org/support/topic/programmatically-add-user-using-wp_insert_user/#post-2185720)
 * same here.. any help would be appreciated!
 *  Thread Starter [EL45](https://wordpress.org/support/users/el45/)
 * (@el45)
 * [14 years, 10 months ago](https://wordpress.org/support/topic/programmatically-add-user-using-wp_insert_user/#post-2185721)
 * [@onelife1985](https://wordpress.org/support/users/onelife1985/) I’m still unsure
   why it wasn’t working before. BUT… I was able to finally get `wp_create_user`
   to work.
 * I accomplished this by installing the plugin as ‘must use’ (ie: in ‘wp-content/
   mu-plugins’ instead of ‘wp-content/plugins’)
 * Also, I switched the ‘hook’ that I was using to create the user. Again, not sure
   why it wasn’t working before.. Wish I had more info for you, but I hope this 
   helps, as I received little support from the community.
 *  [onelife1985](https://wordpress.org/support/users/onelife1985/)
 * (@onelife1985)
 * [14 years, 10 months ago](https://wordpress.org/support/topic/programmatically-add-user-using-wp_insert_user/#post-2185722)
 * [@el45](https://wordpress.org/support/users/el45/) thanks for your reply, indeed
   there is no support about this.. now when you say:
 * “I accomplished this by installing the plugin as ‘must use’ (ie: in ‘wp-content/
   mu-plugins’ instead of ‘wp-content/plugins’)”
 * which plugin are you talking about?
 *  Thread Starter [EL45](https://wordpress.org/support/users/el45/)
 * (@el45)
 * [14 years, 10 months ago](https://wordpress.org/support/topic/programmatically-add-user-using-wp_insert_user/#post-2185724)
 * A custom plugin that I am writing myself. This is where I was trying to make 
   the `wp_create_user` call from. Sorry for the confusion.
 *  [onelife1985](https://wordpress.org/support/users/onelife1985/)
 * (@onelife1985)
 * [14 years, 10 months ago](https://wordpress.org/support/topic/programmatically-add-user-using-wp_insert_user/#post-2185725)
 * I managed to find a way around it.. this is the code I used (for future reference
   as well):
 * global $wpdb;
 * $insertUser = “INSERT INTO $wpdb->users (user_login, user_pass, user_email, user_url)
   VALUES (‘$user_login’, ‘$user_pass’, ‘$user_email’, ‘$user_url’)”;
    $wpdb->query(
   $insertUser );
 * $user_id = $wpdb->get_var($wpdb->prepare(“SELECT id FROM $wpdb->users WHERE user_email
   = %s”, $user_email));
 * $my_user = new WP_User( $user_id );
    $my_user->set_role( “Contributor” );
 * don’t forget to escape all inputs for security reasons..
    Thanks for you time!!
 *  [coombesy](https://wordpress.org/support/users/coombesy/)
 * (@coombesy)
 * [14 years, 3 months ago](https://wordpress.org/support/topic/programmatically-add-user-using-wp_insert_user/#post-2185823)
 * know this is old but had the same problem and after plenty of searching the only
   solution that worked for me was to include pluggable.php:
    `require_once( ABSPATH.'/
   wp-includes/pluggable.php')`
 * i’m using wp 3.1
 * hope this helps someone else
 *  [absingh](https://wordpress.org/support/users/absingh/)
 * (@absingh)
 * [14 years, 1 month ago](https://wordpress.org/support/topic/programmatically-add-user-using-wp_insert_user/#post-2185837)
 * The correct way to enter the password is by using wp_hash_password() function.
   This will ensure that the password entered in the database is actually the hash
   of the password which you want and that is what the wordpress login system actually
   expects.
 *  [alex1982](https://wordpress.org/support/users/alex1982/)
 * (@alex1982)
 * [14 years ago](https://wordpress.org/support/topic/programmatically-add-user-using-wp_insert_user/#post-2185838)
 * As far as I know, if you try to insert new users and pass them an ID, wordpress
   tries to update the user with the given ID.
    See the Example in the reference:
   [http://codex.wordpress.org/Function_Reference/wp_insert_user#Examples](http://codex.wordpress.org/Function_Reference/wp_insert_user#Examples)
   or see the source of the function: [http://core.trac.wordpress.org/browser/tags/3.3.2/wp-includes/user.php#L1260](http://core.trac.wordpress.org/browser/tags/3.3.2/wp-includes/user.php#L1260)
   on line 1260, where it checks for the user-ID. They use wp_insert_user with a
   given ID for updating … so the wording is a bit confusing.
 * In the code from EL45 at pastebin the ID ist set. Remove the ID from the code.
 * Right now I added lots of Users in WordPress 3.1.1 with the following code:
 *     ```
       $user_data = array(
                       'ID' => '',
                       'user_pass' => wp_generate_password(),
                       'user_login' => $loginName,
                       'display_name' => $loginName,
                       'first_name' => $firstName,
                       'last_name' => $lastName,
                       'role' => get_option('default_role') // Use default role or another role, e.g. 'editor'
                   );
                   $user_id = wp_insert_user( $user_data );
                   wp_set_password($lastName, $user_id);
       ```
   
 * and I set up a default password to the user.
    There is no need to set up an email
   address. Required are at least user_pass and user_login but I didn’t tried this
   out.

Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘Programmatically Add User Using wp_insert_user()’ is closed to new replies.

## Tags

 * [add user](https://wordpress.org/support/topic-tag/add-user/)
 * [wp_create_user](https://wordpress.org/support/topic-tag/wp_create_user/)

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 8 replies
 * 5 participants
 * Last reply from: [alex1982](https://wordpress.org/support/users/alex1982/)
 * Last activity: [14 years ago](https://wordpress.org/support/topic/programmatically-add-user-using-wp_insert_user/#post-2185838)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
