add_user_to_blog()
add_user_to_blog( int $blog_id, int $user_id, string $r…
add_user_to_blog( int $blog_id, int $user_id, string $role )
将用户添加到博客。
Adds a user to a blog.
目录锚点:#说明#参数#返回#源码#笔记
说明(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:164 | 5 | 15 |
笔记(Notes)
例子
类别:WordPress 函数手册、
本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。





还没有任何评论,赶紧来占个楼吧!