wppaste
WordPress

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
Inserts a user into the database.

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.
  • $IDint

    User ID. If supplied, the user will be updated.
  • $user_passstring

    The plain-text user password for new users. Hashed password for existing users.
  • $user_loginstring

    The user's login username.
  • $user_nicenamestring

    The URL-friendly user name.
  • $user_urlstring

    The user URL.
  • $user_emailstring

    The user email address.
  • $display_namestringdefault: is the user's username

    The user's display name.
  • $nicknamestringdefault: is the user's username

    The user's nickname.
  • $first_namestring

    The user's first name. For new users, will be used to build the first part of the user's display name if $display_name is not specified.
  • $last_namestring

    The user's last name. For new users, will be used to build the second part of the user's display name if $display_name is not specified.
  • $descriptionstring

    The 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: false

    Whether the user should always access the admin over https.
  • $user_registeredstring

    Date the user registered in UTC. Format is 'Y-m-d H:i:s'.
  • $user_activation_keystringdefault: empty

    Password reset key.
  • $spambooldefault: false

    Multisite 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.
  • $rolestring

    User's role.
  • $localestringdefault: empty

    User's locale.
  • $meta_inputarraydefault: empty

    Array 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

Reaches the database via ->get_var().

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
271–355

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 714.

Plugin surface
18 hooks

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.

Called by
4

4 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()called directly
  • optionoption read or writeget_option()called directly
  • sqldatabase query->get_var()called directly
  • querycontent queryget_user_by()one call below wp_insert_user()
  • cacheobject cachewp_cache_get()one call below wp_insert_user()
  • serializeserialisationmaybe_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.

WhenInstructionsCalls it makes
!($userdata instanceof) && !$userdata && !empty($user_login) && !empty($user_nicename) && defined('WP_IMPORTING') && !is_multisite() && empty($data)271wp_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–342wp_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–347wp_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–348wp_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–341wp_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–345wp_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–349wp_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–346wp_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–354wp_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–355wp_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–348wp_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)352wp_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()353wp_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

PHPCompiledExecutedBranchesNotes
8.6-dev713271–355801 fewer instruction than PHP 8.5
8.5714271–35580
8.4714271–355808 fewer instructions than PHP 8.3
8.3722279–36380
8.2722279–36380
8.1722279–3638014 fewer instructions than PHP 7.4
7.4736293–37780

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:

  1. apply_filters( pre_user_login )filterline 2313 (+92 into the body)

    Filters a username after it has been sanitized.

  2. apply_filters( illegal_user_logins )filterline 2336 (+115 into the body)

    Filters the list of disallowed usernames.

  3. apply_filters( pre_user_nicename )filterline 2361 (+140 into the body)

    Filters a user's nicename before the user is created or updated.

  4. apply_filters( pre_user_email )filterline 2393 (+172 into the body)

    Filters a user's email before the user is created or updated.

  5. apply_filters( pre_user_url )filterline 2416 (+195 into the body)

    Filters a user's URL before the user is created or updated.

  6. apply_filters( pre_user_nickname )filterline 2444 (+223 into the body)

    Filters a user's nickname before the user is created or updated.

  7. 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.

  8. 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.

  9. 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.

  10. apply_filters( pre_user_description )filterline 2507 (+286 into the body)

    Filters a user's description before the user is created or updated.

  11. apply_filters( wp_pre_insert_user_data )filterline 2562 (+341 into the body)

    Filters user data before the record is created or updated.

  12. do_action( wp_set_password )actionline 2582 (+361 into the body)

    Fires after the user password is set.

  13. 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.

  14. 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.

  15. do_action( profile_update )actionline 2678 (+457 into the body)

    Fires immediately after an existing user is updated.

  16. do_action( make_spam_user )actionline 2689 (+468 into the body)

    Fires after the user is marked as a SPAM user.

  17. do_action( make_ham_user )actionline 2698 (+477 into the body)

    Fires after the user is marked as a HAM user. Opposite of SPAM.

  18. do_action( user_register )actionline 2711 (+490 into the body)

    Fires immediately after a new user is registered.

Uses · 23

Show all 23

Used by · 4

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.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

5.9.0
The meta_input field can be passed to $userdata to allow addition of user meta data.from the docblock
5.3.0
The spam field can be passed to $userdata (Multisite only).from the docblock
5.3.0
The user_activation_key field can be passed to $userdata.from the docblock
4.7.0
The locale field can be passed to $userdata.from the docblock
3.6.0
The aim, jabber, and yim fields were removed as default user contact methods for new installations. See wp_get_user_contact_methods().from the docblock
2.0.0
Introduced.from the docblock

About 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.