wp_insert_user [ WordPress Function ]
| Parameters: |
|
| Uses: |
|
| Returns: |
|
| Defined at: |
|
Insert an user into the database.
Can update a current user or insert a new user based on whether the user's ID is present.
Can be used to update the user's info (see below), set the user's role, and set the user's preference on whether they want the rich editor on.
Most of the $userdata array fields have filters associated with the values. The exceptions are 'rich_editing', 'role', 'jabber', 'aim', 'yim', 'user_registered', and 'ID'. The filters have the prefix 'pre_user_' followed by the field name. An example using 'description' would have the filter called, 'pre_user_description' that can be hooked into.
The $userdata array can contain the following fields: 'ID' - An integer that will be used for updating an existing user. 'user_pass' - A string that contains the plain text password for the user. 'user_login' - A string that contains the user's username for logging in. 'user_nicename' - A string that contains a nicer looking name for the user. The default is the user's username. 'user_url' - A string containing the user's URL for the user's web site. 'user_email' - A string containing the user's email address. 'display_name' - A string that will be shown on the site. Defaults to user's username. It is likely that you will want to change this, for appearance. 'nickname' - The user's nickname, defaults to the user's username. 'first_name' - The user's first name. 'last_name' - The user's last name. 'description' - A string containing content about the user. 'rich_editing' - A string for whether to enable the rich editor. False if not empty. 'user_registered' - The date the user registered. Format is 'Y-m-d H:i:s'. 'role' - A string used to set the user's role. 'jabber' - User's Jabber account. 'aim' - User's AOL IM account. 'yim' - User's Yahoo IM account.
Source
<?php
function wp_insert_user($userdata) {
global $wpdb;
extract($userdata, EXTR_SKIP);
// Are we updating or creating?
if ( !empty($ID) ) {
$ID = (int) $ID;
$update = true;
$old_user_data = WP_User::get_data_by( 'id', $ID );
} else {
$update = false;
// Hash the password
$user_pass = wp_hash_password($user_pass);
}
$user_login = sanitize_user($user_login, true);
$user_login = apply_filters('pre_user_login', $user_login);
//Remove any non-printable chars from the login string to see if we have ended up with an empty username
$user_login = trim($user_login);
if ( empty($user_login) )
return new WP_Error('empty_user_login', __('Cannot create a user with an empty login name.') );
if ( !$update && username_exists( $user_login ) )
return new WP_Error('existing_user_login', __('This username is already registered.') );
if ( empty($user_nicename) )
$user_nicename = sanitize_title( $user_login );
$user_nicename = apply_filters('pre_user_nicename', $user_nicename);
if ( empty($user_url) )
$user_url = '';
$user_url = apply_filters('pre_user_url', $user_url);
if ( empty($user_email) )
$user_email = '';
$user_email = apply_filters('pre_user_email', $user_email);
if ( !$update && ! defined( 'WP_IMPORTING' ) && email_exists($user_email) )
return new WP_Error('existing_user_email', __('This email address is already registered.') );
if ( empty($display_name) )
$display_name = $user_login;
$display_name = apply_filters('pre_user_display_name', $display_name);
if ( empty($nickname) )
$nickname = $user_login;
$nickname = apply_filters('pre_user_nickname', $nickname);
if ( empty($first_name) )
$first_name = '';
$first_name = apply_filters('pre_user_first_name', $first_name);
if ( empty($last_name) )
$last_name = '';
$last_name = apply_filters('pre_user_last_name', $last_name);
if ( empty($description) )
$description = '';
$description = apply_filters('pre_user_description', $description);
if ( empty($rich_editing) )
$rich_editing = 'true';
if ( empty($comment_shortcuts) )
$comment_shortcuts = 'false';
if ( empty($admin_color) )
$admin_color = 'fresh';
$admin_color = preg_replace('|[^a-z0-9 _.\-@]|i', '', $admin_color);
if ( empty($use_ssl) )
$use_ssl = 0;
if ( empty($user_registered) )
$user_registered = gmdate('Y-m-d H:i:s');
if ( empty($show_admin_bar_front) )
$show_admin_bar_front = 'true';
$user_nicename_check = $wpdb->get_var( $wpdb->prepare("SELECT ID FROM $wpdb->users WHERE user_nicename = %s AND user_login != %s LIMIT 1" , $user_nicename, $user_login));
if ( $user_nicename_check ) {
$suffix = 2;
while ($user_nicename_check) {
$alt_user_nicename = $user_nicename . "-$suffix";
$user_nicename_check = $wpdb->get_var( $wpdb->prepare("SELECT ID FROM $wpdb->users WHERE user_nicename = %s AND user_login != %s LIMIT 1" , $alt_user_nicename, $user_login));
$suffix++;
}
$user_nicename = $alt_user_nicename;
}
$data = compact( 'user_pass', 'user_email', 'user_url', 'user_nicename', 'display_name', 'user_registered' );
$data = stripslashes_deep( $data );
if ( $update ) {
$wpdb->update( $wpdb->users, $data, compact( 'ID' ) );
$user_id = (int) $ID;
} else {
$wpdb->insert( $wpdb->users, $data + compact( 'user_login' ) );
$user_id = (int) $wpdb->insert_id;
}
$user = new WP_User( $user_id );
foreach ( _get_additional_user_keys( $user ) as $key ) {
if ( isset( $$key ) )
update_user_meta( $user_id, $key, $$key );
}
if ( isset($role) )
$user->set_role($role);
elseif ( !$update )
$user->set_role(get_option('default_role'));
wp_cache_delete($user_id, 'users');
wp_cache_delete($user_login, 'userlogins');
if ( $update )
do_action('profile_update', $user_id, $old_user_data);
else
do_action('user_register', $user_id);
return $user_id;
}
?>
Examples [ wp-snippets.com ]
Top Google Search Results
- Function Reference/wp insert user « WordPress Codex
Description. Insert a user into the database. Can update a ...
codex.wordpress.org - WordPress › Support » wp_insert_user Not Working
Hi there guys, I'm trying to create a form is submited. The code I used to test is this : $userdata = array( 'user_login' => 'login', 'user_email' => 'user@email.com', ...
wordpress.org - [WordPress] and issue with wp_insert_user() and wp_signon ...
Also, when I try the password I used in the array for wp_insert_user() on the login page, it still never work! The user insertion works fine, like its ...
themeforest.net - WordPress wp_insert_user - WordPress Hawaii
Sep 6, 2010 ... http://codex.wordpress.org/Function_Reference/wp_insert_user. Here is some example code for managing both a custom member table and ...
wphawaii.org
User discussions [ wordpress.org ]
- alex1982 on "Programmatically Add User Using wp_insert_user()"
- absingh on "Programmatically Add User Using wp_insert_user()"
- Tahir on "How to trigger registration email when wp_create_user() called"
- Tahir on "How to trigger registration email when wp_create_user() called"
- coombesy on "Programmatically Add User Using wp_insert_user()"
- onelife1985 on "Programmatically Add User Using wp_insert_user()"
- EL45 on "Programmatically Add User Using wp_insert_user()"
- onelife1985 on "Programmatically Add User Using wp_insert_user()"
- EL45 on "Programmatically Add User Using wp_insert_user()"
- onelife1985 on "Programmatically Add User Using wp_insert_user()"