Advertisements

remove_user_from_blog

Definition:
function remove_user_from_blog($user_id, $blog_id = '', $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.

Parameters

  • int $user_id: ID of the user you’re removing.
  • int $blog_id: ID of the blog you’re removing the user from.
  • string $reassign: Optional. A user to whom to reassign posts.

Defined actions

  • remove_user_from_blog
    do_action('remove_user_from_blog', $user_id, $blog_id);

Source code

function remove_user_from_blog($user_id, $blog_id = '', $reassign = '') {

	global $wpdb;

	switch_to_blog($blog_id);

	$user_id = (int) $user_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, 'source_domain', $new_domain);

	}



	// wp_revoke_user($user_id);

	$user = new WP_User($user_id);

	if ( empty( $user->ID ) ) {

		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;

		$wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET post_author = %d WHERE post_author = %d", $reassign, $user_id) );

		$wpdb->query( $wpdb->prepare("UPDATE $wpdb->links SET link_owner = %d WHERE link_owner = %d", $reassign, $user_id) );

	}



	restore_current_blog();



	return true;

}

2731

Advertisements

No comments yet... Be the first to leave a reply!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: