wp_insert_user( array|object|WP_User $userdata ): int|WP_Error
- Since
- 2.0.0, 3.6.0, 4.7.0, 5.3.0, 5.3.0, 5.9.0
- Source
wp-includes/user.php:2221
Description
Most of the $userdata array fields have filters associated with the values. Exceptions are 'ID', 'rich_editing', 'syntax_highlighting', 'comment_shortcuts', 'admin_color', 'use_ssl', 'user_registered', 'user_activation_key', 'spam', and 'role'. The filters have the prefix 'preuser' followed by the field name. An example using 'description' would have the filter called 'pre_user_description' that can be hooked into.
Compatibility
- WordPress
- since 5.9.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$userdataarray|object|WP_User- An array, object, or WP_User object of user data arguments.
$IDintUser ID. If supplied, the user will be updated.$user_passstringThe plain-text user password for new users. Hashed password for existing users.$user_loginstringThe user's login username.$user_nicenamestringThe URL-friendly user name.$user_urlstringThe user URL.$user_emailstringThe user email address.$display_namestringdefault: is the user's usernameThe user's display name.$nicknamestringdefault: is the user's usernameThe user's nickname.$first_namestringThe user's first name. For new users, will be used to build the first part of the user's display name if$display_nameis not specified.$last_namestringThe user's last name. For new users, will be used to build the second part of the user's display name if$display_nameis not specified.$descriptionstringThe user's biographical description.$rich_editingstringdefault: 'true'Whether to enable the rich-editor for the user. Accepts 'true' or 'false' as a string literal, not boolean.$syntax_highlightingstringdefault: 'true'Whether to enable the rich code editor for the user. Accepts 'true' or 'false' as a string literal, not boolean.$comment_shortcutsstringdefault: 'false'Whether to enable comment moderation keyboard shortcuts for the user. Accepts 'true' or 'false' as a string literal, not boolean.$admin_colorstringdefault: 'modern'Admin color scheme for the user.$use_sslbooldefault: falseWhether the user should always access the admin over https.$user_registeredstringDate the user registered in UTC. Format is 'Y-m-d H:i:s'.$user_activation_keystringdefault: emptyPassword reset key.$spambooldefault: falseMultisite only. Whether the user is marked as spam.$show_admin_bar_frontstringdefault: 'true'Whether to display the Admin Bar for the user on the site's front end. Accepts 'true' or 'false' as a string literal, not boolean.$rolestringUser's role.$localestringdefault: emptyUser's locale.$meta_inputarraydefault: emptyArray of custom user meta values keyed by meta key.
Return value
int|WP_Error- The newly created user's ID or a WP_Error object if the user could not be created.
Performance profile
How much work a call to wp_insert_user() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Heavy
- Scaling
- Scales with input
- Instructions
- 271–355
- Plugin surface
- 18 hooks
- Called by
- 4
Reaches the database via ->get_var().
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 714.
Third-party callbacks on 'pre_user_login', 'illegal_user_logins', 'pre_user_nicename' run inside this call, and their cost is not bounded by anything here.
4 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly - optionoption read or write
get_option()called directly - sqldatabase query
->get_var()called directly - querycontent query
get_user_by()one call below wp_insert_user() - cacheobject cache
wp_cache_get()one call below wp_insert_user() - serializeserialisation
maybe_unserialize()one call below wp_insert_user()
Further down the call graph this can also reach transient. That is the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 13 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_insert_user() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!($userdata instanceof) && !$userdata && !empty($user_login) && !empty($user_nicename) && defined('WP_IMPORTING') && !is_multisite() && empty($data) | 271 | wp_hash_password(), sanitize_user(), apply_filters(), mb_strlen(), apply_filters(), strtolower(), array_map(), mb_substr(), sanitize_title(), apply_filters(), mb_strlen(), ->prepare(), ->get_var(), apply_filters(), apply_filters(), mb_strlen(), apply_filters(), apply_filters(), apply_filters(), apply_filters(), apply_filters(), compact(), wp_unslash(), is_multisite(), apply_filters(), __() |
!($userdata instanceof) && !$userdata && !empty($user_login) && !empty($user_nicename) && defined('WP_IMPORTING') && !is_multisite() && !empty($data) && is_array($data) && !isset($userdata) | 325–342 | wp_hash_password(), sanitize_user(), apply_filters(), mb_strlen(), apply_filters(), strtolower(), array_map(), mb_substr(), sanitize_title(), apply_filters(), mb_strlen(), ->prepare(), ->get_var(), apply_filters(), apply_filters(), mb_strlen(), apply_filters(), apply_filters(), apply_filters(), apply_filters(), apply_filters(), compact(), wp_unslash(), is_multisite(), apply_filters(), ->insert(), ->update(), apply_filters(), apply_filters(), array_merge(), wp_get_user_contact_methods(), clean_user_cache(), do_action() |
!($userdata instanceof) && !$userdata && !empty($user_login) && !empty($user_nicename) && defined('WP_IMPORTING') && !is_multisite() && !empty($data) && is_array($data) && isset($userdata) | 330–347 | wp_hash_password(), sanitize_user(), apply_filters(), mb_strlen(), apply_filters(), strtolower(), array_map(), mb_substr(), sanitize_title(), apply_filters(), mb_strlen(), ->prepare(), ->get_var(), apply_filters(), apply_filters(), mb_strlen(), apply_filters(), apply_filters(), apply_filters(), apply_filters(), apply_filters(), compact(), wp_unslash(), is_multisite(), apply_filters(), ->insert(), ->update(), apply_filters(), apply_filters(), array_merge(), wp_get_user_contact_methods(), ->set_role(), clean_user_cache(), do_action() |
!($userdata instanceof) && !$userdata && !empty($user_login) && !empty($user_nicename) && defined('WP_IMPORTING') && !is_multisite() && !empty($data) && is_array($data) && !isset($userdata) | 331–348 | wp_hash_password(), sanitize_user(), apply_filters(), mb_strlen(), apply_filters(), strtolower(), array_map(), mb_substr(), sanitize_title(), apply_filters(), mb_strlen(), ->prepare(), ->get_var(), apply_filters(), apply_filters(), mb_strlen(), apply_filters(), apply_filters(), apply_filters(), apply_filters(), apply_filters(), compact(), wp_unslash(), is_multisite(), apply_filters(), ->insert(), ->update(), apply_filters(), apply_filters(), array_merge(), wp_get_user_contact_methods(), get_option(), ->set_role(), clean_user_cache(), do_action() |
!($userdata instanceof) && !$userdata && !empty($user_login) && !empty($user_nicename) && defined('WP_IMPORTING') && !is_multisite() && !empty($data) && is_array($data) && !isset($userdata) | 333–341 | wp_hash_password(), sanitize_user(), apply_filters(), mb_strlen(), apply_filters(), strtolower(), array_map(), mb_substr(), sanitize_title(), apply_filters(), mb_strlen(), ->prepare(), ->get_var(), apply_filters(), apply_filters(), mb_strlen(), apply_filters(), apply_filters(), apply_filters(), apply_filters(), apply_filters(), compact(), wp_unslash(), is_multisite(), apply_filters(), ->insert(), ->update(), do_action(), apply_filters(), apply_filters(), array_merge(), wp_get_user_contact_methods(), clean_user_cache(), do_action() |
!($userdata instanceof) && !$userdata && !empty($user_login) && !empty($user_nicename) && defined('WP_IMPORTING') && !is_multisite() && !empty($data) && is_array($data) && wp_get_user_contact_methods() && isset($userdata) | 338–345 | wp_hash_password(), sanitize_user(), apply_filters(), mb_strlen(), apply_filters(), strtolower(), array_map(), mb_substr(), sanitize_title(), apply_filters(), mb_strlen(), ->prepare(), ->get_var(), apply_filters(), apply_filters(), mb_strlen(), apply_filters(), apply_filters(), apply_filters(), apply_filters(), apply_filters(), compact(), wp_unslash(), is_multisite(), apply_filters(), ->insert(), ->update(), do_action(), apply_filters(), apply_filters(), array_merge(), wp_get_user_contact_methods(), ->set_role(), clean_user_cache(), do_action() |
!($userdata instanceof) && !$userdata && !empty($user_login) && !empty($user_nicename) && defined('WP_IMPORTING') && !is_multisite() && !empty($data) && is_array($data) | 339–349 | wp_hash_password(), sanitize_user(), apply_filters(), mb_strlen(), apply_filters(), strtolower(), array_map(), mb_substr(), sanitize_title(), apply_filters(), mb_strlen(), ->prepare(), ->get_var(), apply_filters(), apply_filters(), mb_strlen(), apply_filters(), apply_filters(), apply_filters(), apply_filters(), apply_filters(), compact(), wp_unslash(), is_multisite(), apply_filters(), ->insert(), ->update(), apply_filters(), apply_filters(), array_merge(), wp_get_user_contact_methods(), clean_user_cache(), do_action(), do_action() |
!($userdata instanceof) && !$userdata && !empty($user_login) && !empty($user_nicename) && defined('WP_IMPORTING') && !is_multisite() && !empty($data) && is_array($data) && wp_get_user_contact_methods() && !isset($userdata) | 339–346 | wp_hash_password(), sanitize_user(), apply_filters(), mb_strlen(), apply_filters(), strtolower(), array_map(), mb_substr(), sanitize_title(), apply_filters(), mb_strlen(), ->prepare(), ->get_var(), apply_filters(), apply_filters(), mb_strlen(), apply_filters(), apply_filters(), apply_filters(), apply_filters(), apply_filters(), compact(), wp_unslash(), is_multisite(), apply_filters(), ->insert(), ->update(), do_action(), apply_filters(), apply_filters(), array_merge(), wp_get_user_contact_methods(), get_option(), ->set_role(), clean_user_cache(), do_action() |
!($userdata instanceof) && !$userdata && !empty($user_login) && !empty($user_nicename) && defined('WP_IMPORTING') && !is_multisite() && !empty($data) && is_array($data) && isset($userdata) | 344–354 | wp_hash_password(), sanitize_user(), apply_filters(), mb_strlen(), apply_filters(), strtolower(), array_map(), mb_substr(), sanitize_title(), apply_filters(), mb_strlen(), ->prepare(), ->get_var(), apply_filters(), apply_filters(), mb_strlen(), apply_filters(), apply_filters(), apply_filters(), apply_filters(), apply_filters(), compact(), wp_unslash(), is_multisite(), apply_filters(), ->insert(), ->update(), apply_filters(), apply_filters(), array_merge(), wp_get_user_contact_methods(), ->set_role(), clean_user_cache(), do_action(), do_action() |
!($userdata instanceof) && !$userdata && !empty($user_login) && !empty($user_nicename) && defined('WP_IMPORTING') && !is_multisite() && !empty($data) && is_array($data) | 345–355 | wp_hash_password(), sanitize_user(), apply_filters(), mb_strlen(), apply_filters(), strtolower(), array_map(), mb_substr(), sanitize_title(), apply_filters(), mb_strlen(), ->prepare(), ->get_var(), apply_filters(), apply_filters(), mb_strlen(), apply_filters(), apply_filters(), apply_filters(), apply_filters(), apply_filters(), compact(), wp_unslash(), is_multisite(), apply_filters(), ->insert(), ->update(), apply_filters(), apply_filters(), array_merge(), wp_get_user_contact_methods(), get_option(), ->set_role(), clean_user_cache(), do_action(), do_action() |
!($userdata instanceof) && !$userdata && !empty($user_login) && !empty($user_nicename) && defined('WP_IMPORTING') && !is_multisite() && !empty($data) && is_array($data) | 347–348 | wp_hash_password(), sanitize_user(), apply_filters(), mb_strlen(), apply_filters(), strtolower(), array_map(), mb_substr(), sanitize_title(), apply_filters(), mb_strlen(), ->prepare(), ->get_var(), apply_filters(), apply_filters(), mb_strlen(), apply_filters(), apply_filters(), apply_filters(), apply_filters(), apply_filters(), compact(), wp_unslash(), is_multisite(), apply_filters(), ->insert(), ->update(), do_action(), apply_filters(), apply_filters(), array_merge(), wp_get_user_contact_methods(), clean_user_cache(), do_action(), do_action() |
!($userdata instanceof) && !$userdata && !empty($user_login) && !empty($user_nicename) && defined('WP_IMPORTING') && !is_multisite() && !empty($data) && is_array($data) && wp_get_user_contact_methods() && isset($userdata) | 352 | wp_hash_password(), sanitize_user(), apply_filters(), mb_strlen(), apply_filters(), strtolower(), array_map(), mb_substr(), sanitize_title(), apply_filters(), mb_strlen(), ->prepare(), ->get_var(), apply_filters(), apply_filters(), mb_strlen(), apply_filters(), apply_filters(), apply_filters(), apply_filters(), apply_filters(), compact(), wp_unslash(), is_multisite(), apply_filters(), ->insert(), ->update(), do_action(), apply_filters(), apply_filters(), array_merge(), wp_get_user_contact_methods(), ->set_role(), clean_user_cache(), do_action(), do_action() |
1 further outcome, up to 353 instructions
!($userdata instanceof) && !$userdata && !empty($user_login) && !empty($user_nicename) && defined('WP_IMPORTING') && !is_multisite() && !empty($data) && is_array($data) && wp_get_user_contact_methods() | 353 | wp_hash_password(), sanitize_user(), apply_filters(), mb_strlen(), apply_filters(), strtolower(), array_map(), mb_substr(), sanitize_title(), apply_filters(), mb_strlen(), ->prepare(), ->get_var(), apply_filters(), apply_filters(), mb_strlen(), apply_filters(), apply_filters(), apply_filters(), apply_filters(), apply_filters(), compact(), wp_unslash(), is_multisite(), apply_filters(), ->insert(), ->update(), do_action(), apply_filters(), apply_filters(), array_merge(), wp_get_user_contact_methods(), get_option(), ->set_role(), clean_user_cache(), do_action(), do_action() |
This body has more branch combinations than are worth enumerating, so the table covers the outcomes found first rather than every one that exists.
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 713 | 271–355 | 80 | 1 fewer instruction than PHP 8.5 |
| 8.5 | 714 | 271–355 | 80 | |
| 8.4 | 714 | 271–355 | 80 | 8 fewer instructions than PHP 8.3 |
| 8.3 | 722 | 279–363 | 80 | |
| 8.2 | 722 | 279–363 | 80 | |
| 8.1 | 722 | 279–363 | 80 | 14 fewer instructions than PHP 7.4 |
| 7.4 | 736 | 293–377 | 80 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Hooks and filters fired · 18
18 hooks fire while wp_insert_user() runs, in this order:
- apply_filters( pre_user_login )filterline 2313 (+92 into the body)
Filters a username after it has been sanitized.
- apply_filters( illegal_user_logins )filterline 2336 (+115 into the body)
Filters the list of disallowed usernames.
- apply_filters( pre_user_nicename )filterline 2361 (+140 into the body)
Filters a user's nicename before the user is created or updated.
- apply_filters( pre_user_email )filterline 2393 (+172 into the body)
Filters a user's email before the user is created or updated.
- apply_filters( pre_user_url )filterline 2416 (+195 into the body)
Filters a user's URL before the user is created or updated.
- apply_filters( pre_user_nickname )filterline 2444 (+223 into the body)
Filters a user's nickname before the user is created or updated.
- apply_filters( pre_user_first_name )filterline 2455 (+234 into the body)
Filters a user's first name before the user is created or updated.
- apply_filters( pre_user_last_name )filterline 2466 (+245 into the body)
Filters a user's last name before the user is created or updated.
- apply_filters( pre_user_display_name )filterline 2496 (+275 into the body)
Filters a user's display name before the user is created or updated.
- apply_filters( pre_user_description )filterline 2507 (+286 into the body)
Filters a user's description before the user is created or updated.
- apply_filters( wp_pre_insert_user_data )filterline 2562 (+341 into the body)
Filters user data before the record is created or updated.
- do_action( wp_set_password )actionline 2582 (+361 into the body)
Fires after the user password is set.
- apply_filters( insert_user_meta )filterline 2617 (+396 into the body)
Filters a user's meta values and keys immediately after the user is created or updated and before any user meta is inserted or updated.
- apply_filters( insert_custom_user_meta )filterline 2637 (+416 into the body)
Filters a user's custom meta values and keys immediately after the user is created or updated and before any user meta is inserted or updated.
- do_action( profile_update )actionline 2678 (+457 into the body)
Fires immediately after an existing user is updated.
- do_action( make_spam_user )actionline 2689 (+468 into the body)
Fires after the user is marked as a SPAM user.
- do_action( make_ham_user )actionline 2698 (+477 into the body)
Fires after the user is marked as a HAM user. Opposite of SPAM.
- do_action( user_register )actionline 2711 (+490 into the body)
Fires immediately after a new user is registered.
Uses · 23
- get_userdata()Retrieves user info by user ID.
- __()Retrieves the translation of $text.
- wp_slash()Adds slashes to a string or recursively adds slashes to strings within an array.
- wp_trigger_error()Generates a user-level error/warning/notice/deprecation message.
- wp_hash_password()Creates a hash of a plain text password.
- sanitize_user()Sanitizes a username, stripping out unsafe characters.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- mb_strlen()Compat function to mimic mb_strlen().
- username_exists()Determines whether the given username exists.
- mb_substr()Compat function to mimic mb_substr().
- sanitize_title()Sanitizes a string into a slug, which can be used in URLs or HTML attributes.
- email_exists()Determines whether the given email exists.
Show all 23
- is_multisite()Determines whether Multisite is enabled.
- _x()Retrieves translated string with gettext context.
- wp_unslash()Removes slashes from a string or recursively removes slashes from strings within an array.
- do_action()Calls the callback functions that have been added to an action hook.
- update_user_meta()Updates user meta field based on user ID.
- add_user_meta()Adds meta data to a user.
- wp_get_user_contact_methods()Sets up the user contact methods.
- get_option()Retrieves an option value based on an option name.
- clean_user_cache()Cleans all user caches.
- WP_Error::__construct()Initializes the error.
- WP_User::__construct()Constructor.
Used by · 4
- WP_REST_Users_Controller::create_item()Creates a single user.
- edit_user()Edit user settings based on contents of $_POST
- wp_create_user()Provides a simpler way of inserting a user into the database.
- wp_update_user()Updates a user in the database.
Source code
function wp_insert_user( $userdata ) { global $wpdb; if ( $userdata instanceof stdClass ) { $userdata = get_object_vars( $userdata ); } elseif ( $userdata instanceof WP_User ) { $userdata = $userdata->to_array(); } elseif ( $userdata instanceof Traversable ) { $userdata = iterator_to_array( $userdata ); } elseif ( $userdata instanceof ArrayAccess ) { $userdata_obj = $userdata; $userdata = array(); foreach ( array( 'ID', 'user_pass', 'user_login', 'user_nicename', 'user_url', 'user_email', 'display_name', 'nickname', 'first_name', 'last_name', 'description', 'rich_editing', 'syntax_highlighting', 'infinite_scrolling', 'comment_shortcuts', 'admin_color', 'use_ssl', 'user_registered', 'user_activation_key', 'spam', 'show_admin_bar_front', 'role', 'locale', 'meta_input', ) as $key ) { if ( isset( $userdata_obj[ $key ] ) ) { $userdata[ $key ] = $userdata_obj[ $key ]; } } } else { $userdata = (array) $userdata; } // Are we updating or creating? if ( ! empty( $userdata['ID'] ) ) { $user_id = (int) $userdata['ID']; $update = true; $old_user_data = get_userdata( $user_id ); if ( ! $old_user_data ) { return new WP_Error( 'invalid_user_id', __( 'Invalid user ID.' ) ); } // Slash current user email to compare it later with slashed new user email. $old_user_data->user_email = wp_slash( $old_user_data->user_email ); // Hashed in wp_update_user(), plaintext if called directly. $user_pass = ! empty( $userdata['user_pass'] ) ? $userdata['user_pass'] : $old_user_data->user_pass; } else { $update = false; if ( empty( $userdata['user_pass'] ) ) { wp_trigger_error( __FUNCTION__, __( 'The user_pass field is required when creating a new user. The user will need to reset their password before logging in.' ), E_USER_WARNING ); // Set the password as an empty string to force the password reset flow. $userdata['user_pass'] = ''; } // Hash the password. $user_pass = wp_hash_password( $userdata['user_pass'] ); }Changelog
Introduced in 2.0.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
meta_input field can be passed to $userdata to allow addition of user meta data.from the docblockspam field can be passed to $userdata (Multisite only).from the docblockuser_activation_key field can be passed to $userdata.from the docblocklocale field can be passed to $userdata.from the docblockaim, jabber, and yim fields were removed as default user contact methods for new installations. See wp_get_user_contact_methods().from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/user.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.