本文档介绍了在 WordPress 中管理用户的核心函数,包括创建、插入、更新和删除用户的操作。面向开发者,提供了函数使用的基本指南和注意事项。
// 创建用户示例
$user_id = wp_create_user($user_name, $random_password, $user_email);
// 插入用户示例
$user_data = [
'user_login' => $username,
'user_pass' => $password,
'user_url' => $website,
];
$user_id = wp_insert_user($user_data);
// 更新用户示例
$user_id = wp_update_user(array('ID' => $user_id, 'user_url' => $website));To add a user you can use wp_create_user() or wp_insert_user().
wp_create_user() creates a user using only the username, password and email parameters while wp_insert_user() accepts an array or object describing the user and its properties.
wp_create_user() allows you to create a new WordPress user.
Please refer to the Function Reference about wp_create_user() for full explanation about the used parameters.
// check if the username is taken
$user_id = username_exists( $user_name );
// check that the email address does not belong to a registered user
if ( ! $user_id && email_exists( $user_email ) === false ) {
// create a random password
$random_password = wp_generate_password( 12, false );
// create the user
$user_id = wp_create_user(
$user_name,
$random_password,
$user_email
);
}
wp_insert_user( $userdata );
The function performs the action user_register when creating a user (user ID does not exist).
The function performs the action profile_update when updating the user (user ID exists).
Please refer to the Function Reference about wp_insert_user() for full explanation about the used parameters.
Below is an example showing how to insert a new user with the website profile field filled in.
$username = $_POST['username'];
$password = $_POST['password'];
$website = $_POST['website'];
$user_data = [
'user_login' => $username,
'user_pass' => $password,
'user_url' => $website,
];
$user_id = wp_insert_user( $user_data );
// success
if ( ! is_wp_error( $user_id ) ) {
echo 'User created: ' . $user_id;
}
wp_update_user() Updates a single user in the database. The update data is passed along in the $userdata array/object.
To update a single piece of user meta data, use update_user_meta() instead. To create a new user, use wp_insert_user() instead.
Please refer to the Function Reference about wp_update_user() for full explanation about the used parameters.
Below is an example showing how to update a user’s website profile field.
$user_id = 1;
$website = 'https://wordpress.org';
$user_id = wp_update_user(
array(
'ID' => $user_id,
'user_url' => $website,
)
);
if ( is_wp_error( $user_id ) ) {
// error
} else {
// success
}
wp_delete_user() deletes the user and optionally reassign associated entities to another user ID.
deleted_user after the user have been deleted.
Please refer to the Function Reference about wp_delete_user() for full explanation about the used parameters.