add_user_to_blog()

add_user_to_blog()函数是Wordpress函数,将用户添加到博客。

add_user_to_blog( int $blog_id, int $user_id, string $role )

说明(Description)

当用户被添加到博客时,使用“将用户添加到博客”操作触发事件。


参数(Parameters)

参数类型必填说明
$blog_id(int)必需正在向其添加用户的博客的ID。
$user_id(int)必需正在添加的用户的ID。
$role(string)必需希望用户具有的角色

返回(Return)

(true|WP_Error)成功时为true,如果用户不存在或无法添加,则为WP_Error对象。


源码(Source)

/**
 * Add a user to a blog.
 *
 * Use the 'add_user_to_blog' action to fire an event when
 * users are added to a blog.
 *
 * @since MU 1.0
 *
 * @param int    $blog_id ID of the blog you're adding the user to.
 * @param int    $user_id ID of the user you're adding.
 * @param string $role    The role you want the user to have
 * @return true|WP_Error
 */
function add_user_to_blog( $blog_id, $user_id, $role ) {
	switch_to_blog($blog_id);
	$user = get_userdata( $user_id );
	if ( ! $user ) {
		restore_current_blog();
		return new WP_Error( 'user_does_not_exist', __( 'The requested user does not exist.' ) );
	}
	if ( !get_user_meta($user_id, 'primary_blog', true) ) {
		update_user_meta($user_id, 'primary_blog', $blog_id);
		$details = get_blog_details($blog_id);
		update_user_meta($user_id, 'source_domain', $details->domain);
	}
	$user->set_role($role);
	/**
	 * Fires immediately after a user is added to a site.
	 *
	 * @since MU
	 *
	 * @param int    $user_id User ID.
	 * @param string $role    User role.
	 * @param int    $blog_id Blog ID.
	 */
	do_action( 'add_user_to_blog', $user_id, $role, $blog_id );
	wp_cache_delete( $user_id, 'users' );
	wp_cache_delete( $blog_id . '_user_count', 'blog-details' );
	restore_current_blog();
	return true;
}
更新版本源码位置使用被使用
MU (3.0.0)wp-includes/ms-functions.php:164515
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索