add_user_to_blog()函数是Wordpress函数,将用户添加到博客。add_user_to_blog( int $blog_id, int $user_id, string $role
add_user_to_blog()函数是Wordpress函数,将用户添加到博客。
add_user_to_blog( int $blog_id, int $user_id, string $role )
当用户被添加到博客时,使用“将用户添加到博客”操作触发事件。
参数 | 类型 | 必填 | 说明 |
---|---|---|---|
$blog_id | (int) | 必需 | 正在向其添加用户的博客的ID。 |
$user_id | (int) | 必需 | 正在添加的用户的ID。 |
$role | (string) | 必需 | 希望用户具有的角色 |
(true|WP_Error)成功时为true,如果用户不存在或无法添加,则为WP_Error对象。
源码
/** * 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; }
粉丝
0
关注
0
收藏
0