wpmu_delete_user() – Delete a user from the network and remove from all sites.

You appear to be a bot. Output may be restricted

Description

Deletes a user and all of their posts from the network.

This function:

  • Deletes all posts (of all post types) authored by the user on all sites on the network
  • Deletes all links owned by the user on all sites on the network
  • Removes the user from all sites on the network
  • Deletes the user from the database

Usage

$bool = wpmu_delete_user( $id );

Parameters

$id
( int ) required – The user ID.

Returns

bool True if the user was deleted, false otherwise.

Source

File name: wordpress/wp-admin/includes/ms.php
Lines:

1 to 70 of 70
function wpmu_delete_user( $id ) {
  global $wpdb;

  if ( ! is_numeric( $id ) ) {
    return false;
  }

  $id   = (int) $id;
  $user = new WP_User( $id );

  if ( ! $user->exists() ) {
    return false;
  }

  // Global super-administrators are protected, and cannot be deleted.
  $_super_admins = get_super_admins();
  if ( in_array( $user->user_login, $_super_admins, true ) ) {
    return false;
  }

  
/**
 * Fires before a user is deleted from the network.
 *
 * @since MU (3.0.0)
 * @since 5.5.0 Added the `$user` parameter.
 *
 * @param int     $id   ID of the user about to be deleted from the network.
 * @param WP_User $user WP_User object of the user about to be deleted from the network.
 */
  do_action( 'wpmu_delete_user', $id, $user );

  $blogs = get_blogs_of_user( $id );

  if ( ! empty( $blogs ) ) {
    foreach ( $blogs as $blog ) {
      switch_to_blog( $blog->userblog_id );
      remove_user_from_blog( $id, $blog->userblog_id );

      $post_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_author = %d", $id ) );
      foreach ( (array) $post_ids as $post_id ) {
        wp_delete_post( $post_id );
      }

      // Clean links.
      $link_ids = $wpdb->get_col( $wpdb->prepare( "SELECT link_id FROM $wpdb->links WHERE link_owner = %d", $id ) );

      if ( $link_ids ) {
        foreach ( $link_ids as $link_id ) {
          wp_delete_link( $link_id );
        }
      }

      restore_current_blog();
    }
  }

  $meta = $wpdb->get_col( $wpdb->prepare( "SELECT umeta_id FROM $wpdb->usermeta WHERE user_id = %d", $id ) );
  foreach ( $meta as $mid ) {
    delete_metadata_by_mid( 'user', $mid );
  }

  $wpdb->delete( $wpdb->users, array( 'ID' => $id ) );

  clean_user_cache( $user );

  
/** This action is documented in wp-admin/includes/user.php */
  do_action( 'deleted_user', $id, null, $user );

  return true;
}
 

 View on GitHub View on Trac