wp_delete_user

Remove user and optionally reassign posts and links to another user.

Description

wp_delete_user( (int) $id, (null) $reassign = null ); 

If the $reassign parameter is not assigned to a User ID, then all posts will be deleted of that user. The action that is passed the User ID being deleted will be run after the posts are either reassigned or deleted. The user meta will also be deleted that are for that User ID.

Parameters (2)

0. $id (int)
User ID.
1. $reassign — Optional. (null) => null
Reassign posts and links to new User ID.

Usage

  1. if ( !function_exists( 'wp_delete_user' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/user.php'; 
  3.  
  4. // User ID. 
  5. $id = -1; 
  6.  
  7. // Optional. Reassign posts and links to new User ID. 
  8. $reassign = null; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = wp_delete_user($id, $reassign); 
  12.  

Defined (1)

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

/wp-admin/includes/user.php  
  1. function wp_delete_user( $id, $reassign = null ) { 
  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. // Normalize $reassign to null or a user ID. 'novalue' was an older default. 
  14. if ( 'novalue' === $reassign ) { 
  15. $reassign = null; 
  16. } elseif ( null !== $reassign ) { 
  17. $reassign = (int) $reassign; 
  18.  
  19. /** 
  20. * Fires immediately before a user is deleted from the database. 
  21. * @since 2.0.0 
  22. * @param int $id ID of the user to delete. 
  23. * @param int|null $reassign ID of the user to reassign posts and links to. 
  24. * Default null, for no reassignment. 
  25. */ 
  26. do_action( 'delete_user', $id, $reassign ); 
  27.  
  28. if ( null === $reassign ) { 
  29. $post_types_to_delete = array(); 
  30. foreach ( get_post_types( array(), 'objects' ) as $post_type ) { 
  31. if ( $post_type->delete_with_user ) { 
  32. $post_types_to_delete[] = $post_type->name; 
  33. } elseif ( null === $post_type->delete_with_user && post_type_supports( $post_type->name, 'author' ) ) { 
  34. $post_types_to_delete[] = $post_type->name; 
  35.  
  36. /** 
  37. * Filters the list of post types to delete with a user. 
  38. * @since 3.4.0 
  39. * @param array $post_types_to_delete Post types to delete. 
  40. * @param int $id User ID. 
  41. */ 
  42. $post_types_to_delete = apply_filters( 'post_types_to_delete_with_user', $post_types_to_delete, $id ); 
  43. $post_types_to_delete = implode( "', '", $post_types_to_delete ); 
  44. $post_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_author = %d AND post_type IN ('$post_types_to_delete')", $id ) ); 
  45. if ( $post_ids ) { 
  46. foreach ( $post_ids as $post_id ) 
  47. wp_delete_post( $post_id ); 
  48.  
  49. // Clean links 
  50. $link_ids = $wpdb->get_col( $wpdb->prepare("SELECT link_id FROM $wpdb->links WHERE link_owner = %d", $id) ); 
  51.  
  52. if ( $link_ids ) { 
  53. foreach ( $link_ids as $link_id ) 
  54. wp_delete_link($link_id); 
  55. } else { 
  56. $post_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_author = %d", $id ) ); 
  57. $wpdb->update( $wpdb->posts, array('post_author' => $reassign), array('post_author' => $id) ); 
  58. if ( ! empty( $post_ids ) ) { 
  59. foreach ( $post_ids as $post_id ) 
  60. clean_post_cache( $post_id ); 
  61. $link_ids = $wpdb->get_col( $wpdb->prepare("SELECT link_id FROM $wpdb->links WHERE link_owner = %d", $id) ); 
  62. $wpdb->update( $wpdb->links, array('link_owner' => $reassign), array('link_owner' => $id) ); 
  63. if ( ! empty( $link_ids ) ) { 
  64. foreach ( $link_ids as $link_id ) 
  65. clean_bookmark_cache( $link_id ); 
  66.  
  67. // FINALLY, delete user 
  68. if ( is_multisite() ) { 
  69. } else { 
  70. $meta = $wpdb->get_col( $wpdb->prepare( "SELECT umeta_id FROM $wpdb->usermeta WHERE user_id = %d", $id ) ); 
  71. foreach ( $meta as $mid ) 
  72. delete_metadata_by_mid( 'user', $mid ); 
  73.  
  74. $wpdb->delete( $wpdb->users, array( 'ID' => $id ) ); 
  75.  
  76. clean_user_cache( $user ); 
  77.  
  78. /** 
  79. * Fires immediately after a user is deleted from the database. 
  80. * @since 2.9.0 
  81. * @param int $id ID of the deleted user. 
  82. * @param int|null $reassign ID of the user to reassign posts and links to. 
  83. * Default null, for no reassignment. 
  84. */ 
  85. do_action( 'deleted_user', $id, $reassign ); 
  86.  
  87. return true;