Insert a user into the database. For updating an existing user, use wp_update_user function instead.

wp_insert_user() 描述


<?php wp_insert_user$userdata ); ?>

wp_insert_user() 用法



(mixed) (必填) An array of user data, stdClass or WP_User object.

默认值: None

wp_insert_user() 参数



  • If successful, returns the newly-created user's user_id
  • If query fails returns a WP_Error object
  • If user_login is longer than 60 symbols, returns 0.

wp_insert_user() 返回值


Below is an example showing how to insert a new user with the website profile field filled.

wp_insert_user() 示例


  • 使用到: $wpdb WordPress database layer.
  • 使用到: apply_filters() 调用 过滤器s for most of the $userdata fields with the prefix 'pre_user'. See description above.
  • 使用到: do_action() 调用 'profile_update' hook when updating giving the user's ID
  • 使用到: do_action() 调用 'user_register' hook when creating a new user giving the user's ID
The $userdata array can contain the following fields
Field Name Description Associated Filter
ID An integer that will be used for updating an existing user. (none)
user_pass A string that contains the plain text password for the user. pre_user_pass
user_login A string that contains the user's username for logging in. pre_user_login
user_nicename A string that contains a URL-friendly name for the user. The default is the user's username. pre_user_nicename
user_url A string containing the user's URL for the user's web site. pre_user_url
user_email A string containing the user's email address. pre_user_email
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 both appearance and security through obscurity (that is if you dont use and delete the default admin user). pre_user_display_name
nickname The user's nickname, defaults to the user's username. pre_user_nickname
first_name The user's first name. pre_user_first_name
last_name The user's last name. pre_user_last_name
description A string containing content about the user. pre_user_description
rich_editing A string for whether to enable the rich editor or not. False if not empty. (none)
user_registered The date the user registered. Format is Y-m-d H:i:s. (none)
role A string used to set the user's role. (none)
jabber User's Jabber account. (none)
aim User's AOL IM account. (none)
yim User's Yahoo IM account. (none)

If there is no ID, a new user will be created.
If you pass an ID , the user with that ID will be updated, and these meta fields are updated if set in $userdata otherwise they are set to null:

When performing an update operation using wp_insert_user, user_pass should be the hashed password and not the plain text password.

wp_insert_user() 注意


  • 3.5.0: Now accepts stdClass or WP_User object
  • 添加于 版本: 2.0.0

wp_insert_user() 历史


As of 3.1 wp_insert_user() 函数的代码位于 wp-includes/user.php.

wp_insert_user() 源文件