插件开发文档

💡 云策文档标注

概述

本文档介绍了在 WordPress 中管理用户的核心函数,包括创建、插入、更新和删除用户的操作。面向开发者,提供了函数使用的基本指南和注意事项。

关键要点

  • 使用 wp_create_user() 或 wp_insert_user() 添加用户,前者仅需用户名、密码和邮箱,后者接受数组或对象参数。
  • wp_update_user() 用于更新用户信息,更新密码时会清除 cookies,更新单个元数据应使用 update_user_meta()。
  • wp_delete_user() 删除用户并可选择重新分配关联内容,未设置 $reassign 参数将删除所有相关实体。
  • 函数调用时会触发相关 Hook,如 user_register、profile_update 和 deleted_user。

代码示例

// 创建用户示例
$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));

注意事项

  • 使用前应检查用户名和邮箱是否已存在,避免重复创建。
  • 操作后建议检查返回值是否为 WP_Error 以处理错误情况。
  • 详细参数说明请参考相关函数的官方文档。

📄 原文内容

Adding Users

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.

Create User

wp_create_user() allows you to create a new WordPress user.

It uses wp_slash() to escape the values. The PHP compact() function to create an array with these values. The wp_insert_user() to perform the insert operation.

Please refer to the Function Reference about wp_create_user() for full explanation about the used parameters.

Example Create

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

Insert User

wp_insert_user( $userdata );
The function calls a filter for most predefined properties.

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.

Example Insert

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;
}

Updating Users

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.

If current user’s password is being updated, then the cookies will be cleared!

Please refer to the Function Reference about wp_update_user() for full explanation about the used parameters.

Example Update

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
}

Deleting Users

wp_delete_user() deletes the user and optionally reassign associated entities to another user ID.

The function performs the action deleted_user after the user have been deleted.
If the $reassign parameter is not set to a valid user ID, then all entities belonging to the deleted user will be deleted!

Please refer to the Function Reference about wp_delete_user() for full explanation about the used parameters.