remove_user_from_blog

Remove a user from a blog.

Description

(true|WP_Error) remove_user_from_blog( (int) $user_id, (string) $blog_id = '', (string) $reassign = '' ); 

Use the 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.

Returns (true|WP_Error)

Parameters (3)

0. $user_id (int)
ID of the user you're removing.
1. $blog_id — Optional. (string) => ''
ID of the blog you're removing the user from.
2. $reassign — Optional. (string) => ''
A user to whom to reassign posts.

Usage

  1. if ( !function_exists( 'remove_user_from_blog' ) ) { 
  2. require_once ABSPATH . WPINC . '/ms-functions.php'; 
  3.  
  4. // ID of the user you're removing. 
  5. $user_id = -1; 
  6.  
  7. // ID of the blog you're removing the user from. 
  8. $blog_id = ''; 
  9.  
  10. // Optional. A user to whom to reassign posts. 
  11. $reassign = ''; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = remove_user_from_blog($user_id, $blog_id, $reassign); 
  15.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/ms-functions.php  
  1. function remove_user_from_blog($user_id, $blog_id = '', $reassign = '') { 
  2. global $wpdb; 
  3. switch_to_blog($blog_id); 
  4. $user_id = (int) $user_id; 
  5. /** 
  6. * Fires before a user is removed from a site. 
  7. * @since MU 
  8. * @param int $user_id User ID. 
  9. * @param int $blog_id Blog ID. 
  10. */ 
  11. do_action( 'remove_user_from_blog', $user_id, $blog_id ); 
  12.  
  13. // If being removed from the primary blog, set a new primary if the user is assigned 
  14. // to multiple blogs. 
  15. $primary_blog = get_user_meta($user_id, 'primary_blog', true); 
  16. if ( $primary_blog == $blog_id ) { 
  17. $new_id = ''; 
  18. $new_domain = ''; 
  19. $blogs = get_blogs_of_user($user_id); 
  20. foreach ( (array) $blogs as $blog ) { 
  21. if ( $blog->userblog_id == $blog_id ) 
  22. continue; 
  23. $new_id = $blog->userblog_id; 
  24. $new_domain = $blog->domain; 
  25. break; 
  26.  
  27. update_user_meta($user_id, 'primary_blog', $new_id); 
  28. update_user_meta($user_id, 'source_domain', $new_domain); 
  29.  
  30. // wp_revoke_user($user_id); 
  31. $user = get_userdata( $user_id ); 
  32. if ( ! $user ) { 
  33. return new WP_Error('user_does_not_exist', __('That user does not exist.')); 
  34.  
  35. $user->remove_all_caps(); 
  36.  
  37. $blogs = get_blogs_of_user($user_id); 
  38. if ( count($blogs) == 0 ) { 
  39. update_user_meta($user_id, 'primary_blog', ''); 
  40. update_user_meta($user_id, 'source_domain', ''); 
  41.  
  42. if ( $reassign != '' ) { 
  43. $reassign = (int) $reassign; 
  44. $post_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_author = %d", $user_id ) ); 
  45. $link_ids = $wpdb->get_col( $wpdb->prepare( "SELECT link_id FROM $wpdb->links WHERE link_owner = %d", $user_id ) ); 
  46.  
  47. if ( ! empty( $post_ids ) ) { 
  48. $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET post_author = %d WHERE post_author = %d", $reassign, $user_id ) ); 
  49. array_walk( $post_ids, 'clean_post_cache' ); 
  50.  
  51. if ( ! empty( $link_ids ) ) { 
  52. $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->links SET link_owner = %d WHERE link_owner = %d", $reassign, $user_id ) ); 
  53. array_walk( $link_ids, 'clean_bookmark_cache' ); 
  54.  
  55.  
  56. return true;