wp_insert_user() – Inserts a user into the database.

You appear to be a bot. Output may be restricted

Description

Inserts a user into the database.

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 'pre_user_' followed by the field name. An example using 'description' would have the filter called 'pre_user_description' that can be hooked into.

Usage

$int|WP_Error = wp_insert_user( $userdata );

Parameters

$userdata
( array|object|WP_User ) required – { An array, object, or WP_User object of user data arguments.
$ID
( int ) required – User ID. If supplied, the user will be updated.
$user_pass
( string ) required – The plain-text user password for new users. Hashed password for existing users.
$user_login
( string ) required – The user's login username.
$user_nicename
( string ) required – The URL-friendly user name.
$user_url
( string ) required – The user URL.
$user_email
( string ) required – The user email address.
$display_name
( string ) required – The user's display name. Default is the user's username.
$nickname
( string ) required – The user's nickname. Default is the user's username.
$first_name
( string ) required – 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_name
( string ) required – 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.
$description
( string ) required – The user's biographical description.
$rich_editing
( string ) required – Whether to enable the rich-editor for the user. Accepts 'true' or 'false' as a string literal, not boolean. Default 'true'.
$syntax_highlighting
( string ) required – Whether to enable the rich code editor for the user. Accepts 'true' or 'false' as a string literal, not boolean. Default 'true'.
$comment_shortcuts
( string ) required – Whether to enable comment moderation keyboard shortcuts for the user. Accepts 'true' or 'false' as a string literal, not boolean. Default 'false'.
$admin_color
( string ) required – Admin color scheme for the user. Default 'fresh'.
$use_ssl
( bool ) required – Whether the user should always access the admin over https. Default false.
$user_registered
( string ) required – Date the user registered in UTC. Format is 'Y-m-d H:i:s'.
$user_activation_key
( string ) required – Password reset key. Default empty.
$spam
( bool ) required – Multisite only. Whether the user is marked as spam. Default false.
$show_admin_bar_front
( string ) required – 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. Default 'true'.
$role
( string ) required – User's role.
$locale
( string ) required – User's locale. Default empty.
$meta_input
( array ) required – Array of custom user meta values keyed by meta key. Default empty. }

Returns

int|WP_Error The newly created user's ID or a WP_Error object if the user could not be created.

Source

File name: wordpress/wp-includes/user.php
Lines:

1 to 100 of 430
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();
  }

  // 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.' ) );
    }

    // 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;
    // Hash the password.
    $user_pass = wp_hash_password( $userdata['user_pass'] );
  }

  $sanitized_user_login = sanitize_user( $userdata['user_login'], true );

  
/**
 * Filters a username after it has been sanitized.
 *
 * This filter is called before the user is created or updated.
 *
 * @since 2.0.3
 *
 * @param string $sanitized_user_login Username after it has been sanitized.
 */
  $pre_user_login = apply_filters( 'pre_user_login', $sanitized_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( $pre_user_login );

  // user_login must be between 0 and 60 characters.
  if ( empty( $user_login ) ) {
    return new WP_Error( 'empty_user_login', __( 'Cannot create a user with an empty login name.' ) );
  } elseif ( mb_strlen( $user_login ) > 60 ) {
    return new WP_Error( 'user_login_too_long', __( 'Username may not be longer than 60 characters.' ) );
  }

  if ( ! $update && username_exists( $user_login ) ) {
    return new WP_Error( 'existing_user_login', __( 'Sorry, that username already exists!' ) );
  }

  
/**
 * Filters the list of disallowed usernames.
 *
 * @since 4.4.0
 *
 * @param array $usernames Array of disallowed usernames.
 */
  $illegal_logins = (array) apply_filters( 'illegal_user_logins', array() );

  if ( in_array( strtolower( $user_login ), array_map( 'strtolower', $illegal_logins ), true ) ) {
    return new WP_Error( 'invalid_username', __( 'Sorry, that username is not allowed.' ) );
  }

  /*
	 * If a nicename is provided, remove unsafe user characters before using it.
	 * Otherwise build a nicename from the user_login.
	 */
  if ( ! empty( $userdata['user_nicename'] ) ) {
    $user_nicename = sanitize_user( $userdata['user_nicename'], true );
  } else {
    $user_nicename = mb_substr( $user_login, 0, 50 );
  }

  $user_nicename = sanitize_title( $user_nicename );

  
/**
 * Filters a user's nicename before the user is created or updated.
 *
 * @since 2.0.3
 *
 * @param string $user_nicename The user's nicename.
 */
  $user_nicename = apply_filters( 'pre_user_nicename', $user_nicename );

  if ( mb_strlen( $user_nicename ) > 50 ) {
    return new WP_Error( 'user_nicename_too_long', __( 'Nicename may not be longer than 50 characters.' ) );
  }

  $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 ) {
      // user_nicename allows 50 chars. Subtract one for a hyphen, plus the length of the suffix.
      $base_length         = 49 - mb_strlen( $suffix );
      $alt_user_nicename   = mb_substr( $user_nicename, 0, $base_length ) . "-$suffix";
 

 View on GitHub View on Trac