wpmu_delete_user

Delete a user from the network and remove from all sites.

Description

wpmu_delete_user( (int) $id ); 

Parameters (1)

0. $id (int)
The user ID.

Usage

  1. if ( !function_exists( 'wpmu_delete_user' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/ms.php'; 
  3.  
  4. // The user ID. 
  5. $id = -1; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = wpmu_delete_user($id); 
  9.  

Defined (1)

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

/wp-admin/includes/ms.php  
  1. function wpmu_delete_user( $id ) { 
  2. global $wpdb; 
  3.  
  4. if ( ! is_numeric( $id ) ) { 
  5. return false; 
  6.  
  7. $id = (int) $id; 
  8. $user = new WP_User( $id ); 
  9.  
  10. if ( !$user->exists() ) 
  11. return false; 
  12.  
  13. // Global super-administrators are protected, and cannot be deleted. 
  14. $_super_admins = get_super_admins(); 
  15. if ( in_array( $user->user_login, $_super_admins, true ) ) { 
  16. return false; 
  17.  
  18. /** 
  19. * Fires before a user is deleted from the network. 
  20. * @since MU 
  21. * @param int $id ID of the user about to be deleted from the network. 
  22. */ 
  23.  
  24. $blogs = get_blogs_of_user( $id ); 
  25.  
  26. if ( ! empty( $blogs ) ) { 
  27. foreach ( $blogs as $blog ) { 
  28. switch_to_blog( $blog->userblog_id ); 
  29. remove_user_from_blog( $id, $blog->userblog_id ); 
  30.  
  31. $post_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_author = %d", $id ) ); 
  32. foreach ( (array) $post_ids as $post_id ) { 
  33. wp_delete_post( $post_id ); 
  34.  
  35. // Clean links 
  36. $link_ids = $wpdb->get_col( $wpdb->prepare( "SELECT link_id FROM $wpdb->links WHERE link_owner = %d", $id ) ); 
  37.  
  38. if ( $link_ids ) { 
  39. foreach ( $link_ids as $link_id ) 
  40. wp_delete_link( $link_id ); 
  41.  
  42.  
  43. $meta = $wpdb->get_col( $wpdb->prepare( "SELECT umeta_id FROM $wpdb->usermeta WHERE user_id = %d", $id ) ); 
  44. foreach ( $meta as $mid ) 
  45. delete_metadata_by_mid( 'user', $mid ); 
  46.  
  47. $wpdb->delete( $wpdb->users, array( 'ID' => $id ) ); 
  48.  
  49. clean_user_cache( $user ); 
  50.  
  51. /** This action is documented in wp-admin/includes/user.php */ 
  52.  
  53. return true;