remove_user_from_blog()
云策文档标注
概述
remove_user_from_blog() 函数用于从博客中移除用户,支持可选参数以在移除时重新分配用户的文章。函数执行时会触发 'remove_user_from_blog' 动作钩子,并处理用户权限、主博客设置等逻辑。
关键要点
- 函数签名:remove_user_from_blog( $user_id, $blog_id = 0, $reassign = 0 )
- 参数:$user_id(必需,用户ID),$blog_id(可选,博客ID,默认0),$reassign(可选,重新分配文章的用户ID,默认0)
- 返回值:成功时返回 true,用户不存在时返回 WP_Error 对象
- 触发动作钩子:do_action( 'remove_user_from_blog', $user_id, $blog_id, $reassign )
- 内部逻辑:包括切换博客、更新主博客元数据、移除用户权限、重新分配文章和链接等
代码示例
function remove_user_from_blog( $user_id, $blog_id = 0, $reassign = 0 ) {
global $wpdb;
$user_id = (int) $user_id;
$blog_id = (int) $blog_id;
switch_to_blog( $blog_id );
do_action( 'remove_user_from_blog', $user_id, $blog_id, $reassign );
// 处理主博客逻辑
$primary_blog = (int) get_user_meta( $user_id, 'primary_blog', true );
if ( $primary_blog === $blog_id ) {
$blogs = get_blogs_of_user( $user_id );
foreach ( (array) $blogs as $blog ) {
if ( $blog->userblog_id === $blog_id ) {
continue;
}
update_user_meta( $user_id, 'primary_blog', $blog->userblog_id );
update_user_meta( $user_id, 'source_domain', $blog->domain );
break;
}
}
$user = get_userdata( $user_id );
if ( ! $user ) {
restore_current_blog();
return new WP_Error( 'user_does_not_exist', __( 'That user does not exist.' ) );
}
$user->remove_all_caps();
$blogs = get_blogs_of_user( $user_id );
if ( count( $blogs ) === 0 ) {
update_user_meta( $user_id, 'primary_blog', '' );
update_user_meta( $user_id, 'source_domain', '' );
}
if ( $reassign ) {
$reassign = (int) $reassign;
$post_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_author = %d", $user_id ) );
$link_ids = $wpdb->get_col( $wpdb->prepare( "SELECT link_id FROM $wpdb->links WHERE link_owner = %d", $user_id ) );
if ( ! empty( $post_ids ) ) {
$wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET post_author = %d WHERE post_author = %d", $reassign, $user_id ) );
array_walk( $post_ids, 'clean_post_cache' );
}
if ( ! empty( $link_ids ) ) {
$wpdb->query( $wpdb->prepare( "UPDATE $wpdb->links SET link_owner = %d WHERE link_owner = %d", $reassign, $user_id ) );
array_walk( $link_ids, 'clean_bookmark_cache' );
}
}
clean_user_cache( $user_id );
restore_current_blog();
return true;
}注意事项
- 函数在 WordPress MU(3.0.0)中引入,5.4.0 版本添加了 $reassign 参数
- 移除用户时,如果用户是多个博客的成员,会更新主博客元数据
- 如果 $reassign 参数指定,用户的文章和链接将被重新分配给其他用户
- 函数执行后,会清理用户缓存并恢复当前博客上下文
原文内容
Removes a user from a blog.
Description
Use the ‘remove_user_from_blog’ action to fire an event when users are removed from a blog.
Accepts an optional $reassign parameter, if you want to reassign the user’s blog posts to another user upon removal.
Parameters
$user_idintrequired-
ID of the user being removed.
$blog_idintoptional-
ID of the blog the user is being removed from. Default 0.
$reassignintoptional-
ID of the user to whom to reassign posts. Default 0.
Source
function remove_user_from_blog( $user_id, $blog_id = 0, $reassign = 0 ) {
global $wpdb;
$user_id = (int) $user_id;
$blog_id = (int) $blog_id;
switch_to_blog( $blog_id );
/**
* Fires before a user is removed from a site.
*
* @since MU (3.0.0)
* @since 5.4.0 Added the `$reassign` parameter.
*
* @param int $user_id ID of the user being removed.
* @param int $blog_id ID of the blog the user is being removed from.
* @param int $reassign ID of the user to whom to reassign posts.
*/
do_action( 'remove_user_from_blog', $user_id, $blog_id, $reassign );
/*
* If being removed from the primary blog, set a new primary
* if the user is assigned to multiple blogs.
*/
$primary_blog = (int) get_user_meta( $user_id, 'primary_blog', true );
if ( $primary_blog === $blog_id ) {
$new_id = '';
$new_domain = '';
$blogs = get_blogs_of_user( $user_id );
foreach ( (array) $blogs as $blog ) {
if ( $blog->userblog_id === $blog_id ) {
continue;
}
$new_id = $blog->userblog_id;
$new_domain = $blog->domain;
break;
}
update_user_meta( $user_id, 'primary_blog', $new_id );
update_user_meta( $user_id, 'source_domain', $new_domain );
}
$user = get_userdata( $user_id );
if ( ! $user ) {
restore_current_blog();
return new WP_Error( 'user_does_not_exist', __( 'That user does not exist.' ) );
}
$user->remove_all_caps();
$blogs = get_blogs_of_user( $user_id );
if ( count( $blogs ) === 0 ) {
update_user_meta( $user_id, 'primary_blog', '' );
update_user_meta( $user_id, 'source_domain', '' );
}
if ( $reassign ) {
$reassign = (int) $reassign;
$post_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_author = %d", $user_id ) );
$link_ids = $wpdb->get_col( $wpdb->prepare( "SELECT link_id FROM $wpdb->links WHERE link_owner = %d", $user_id ) );
if ( ! empty( $post_ids ) ) {
$wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET post_author = %d WHERE post_author = %d", $reassign, $user_id ) );
array_walk( $post_ids, 'clean_post_cache' );
}
if ( ! empty( $link_ids ) ) {
$wpdb->query( $wpdb->prepare( "UPDATE $wpdb->links SET link_owner = %d WHERE link_owner = %d", $reassign, $user_id ) );
array_walk( $link_ids, 'clean_bookmark_cache' );
}
}
clean_user_cache( $user_id );
restore_current_blog();
return true;
}
Hooks
- do_action( ‘remove_user_from_blog’, int $user_id, int $blog_id, int $reassign )
-
Fires before a user is removed from a site.
Changelog
| Version | Description |
|---|---|
| MU (3.0.0) | Introduced. |