remove_user_from_blog() - WordPress 函数参考中文文档

返回介绍

remove_user_from_blog()

发布于 2017-09-11 字数4591 浏览 790 评论 0

remove_user_from_blog( int $user_id,  int $blog_id = '',  string $reassign = '' )

Remove a user from a blog.


描述

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.


参数

$user_id

(int)
(Required)
ID of the user you’re removing.

$blog_id

(int)
(Optional)
ID of the blog you’re removing the user from.

Default value: ”

$reassign

(string)
(Optional)
A user to whom to reassign posts.

Default value: ”


返回值

(true|WP_Error)


源代码

File: wp-includes/ms-functions.php

function remove_user_from_blog($user_id, $blog_id = '', $reassign = '') {
	global $wpdb;
	switch_to_blog($blog_id);
	$user_id = (int) $user_id;
	/**
	 * Fires before a user is removed from a site.
	 *
	 * @since MU
	 *
	 * @param int $user_id User ID.
	 * @param int $blog_id Blog ID.
	 */
	do_action( 'remove_user_from_blog', $user_id, $blog_id );

	// 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, '源代码_domain', $new_domain);
	}

	// wp_revoke_user($user_id);
	$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, '源代码_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;
}

更新日志

Version 描述
MU 1.0 Introduced.

相关函数

Uses

  • wp-includes/l10n.php:
    __()
  • wp-includes/pluggable.php:
    get_userdata()
  • wp-includes/plugin.php:
    do_action()
  • wp-includes/user.php:
    get_user_meta()
  • wp-includes/user.php:
    get_blogs_of_user()
  • wp-includes/user.php:
    update_user_meta()
  • wp-includes/ms-functions.php:
    remove_user_from_blog
  • wp-includes/ms-blogs.php:
    switch_to_blog()
  • wp-includes/ms-blogs.php:
    restore_current_blog()
  • wp-includes/wp-db.php:
    wpdb::get_col()
  • wp-includes/wp-db.php:
    wpdb::query()
  • wp-includes/wp-db.php:
    wpdb::prepare()
  • wp-includes/class-wp-error.php:
    WP_Error::__construct()
  • Show 8 more uses
    Hide more uses


Used By

  • wp-admin/includes/ms.php:
    wpmu_delete_user()
  • wp-admin/includes/ms.php:
    wpmu_delete_blog()
  • wp-admin/includes/user.php:
    wp_delete_user()
  • wp-includes/ms-functions.php:
    add_new_user_to_blog()

User Contributed Notes

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

目前还没有任何评论,快来抢沙发吧!