remove_user_from_blog

函数
remove_user_from_blog ( $user_id, $blog_id = 0, $reassign = 0 )
参数
  • (int) $user_id ID of the user being removed.
    Required:
  • (int) $blog_id Optional. ID of the blog the user is being removed from. Default 0.
    Required:
  • (int) $reassign Optional. ID of the user to whom to reassign posts. Default 0.
    Required:
返回值
  • (true|WP_Error) True on success or a WP_Error object if the user doesn't exist.
定义位置
相关方法
remove_query_argget_users_of_blogcurrent_user_can_for_blogadd_user_to_blogrestore_current_blog
引入
-
弃用
-

remove_user_from_blog: 这个函数用来从一个WordPress网站上删除一个用户: 这个函数需要两个参数,第一个是用户的ID,第二个是网站的ID。

将一个用户从博客中删除。

使用{@see ‘remove_user_from_blog’}动作,当用户从博客中被删除时触发一个事件。

接受一个可选的`$reassign’参数,如果你想在删除时将用户的博客文章重新分配给其他用户。

function remove_user_from_blog( $user_id, $blog_id = 0, $reassign = 0 ) {
	global $wpdb;

	switch_to_blog( $blog_id );
	$user_id = (int) $user_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 = 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' );
		}
	}

	restore_current_blog();

	return true;
}

常见问题

FAQs
查看更多 >