Using WordPress ‘confirm_delete_users()’ PHP function

The confirm_delete_users() WordPress PHP function is used to confirm the deletion of users. It’s typically used for users who have a certain role, like ‘subscriber’.

Usage

Here’s a basic example of how to use the confirm_delete_users() function:

$args = array('role' => 'subscriber'); 
$subscribers = get_users($args); 
$user_ids = array();

foreach ($subscribers as $user) { 
  $user_ids[] = $user->ID; 
}

confirm_delete_users($user_ids);

In this example, the function is used to delete all users with the ‘subscriber’ role.

Parameters

  • $users (array) (Required): This is an array of user IDs that you want to delete.

More information

See WordPress Developer Resources: confirm_delete_users()

Examples

Deleting Specific Users

// Specify the user IDs to delete
$user_ids = array(1, 3, 45, 7, 378);

// Use the function to delete the users
confirm_delete_users($user_ids);

This code deletes the users with the IDs specified in the array.

Deleting Users with a Specific Role

$args = array('role' => 'editor'); 
$editors = get_users($args); 
$editor_ids = array();

foreach ($editors as $user) { 
  $editor_ids[] = $user->ID; 
}

confirm_delete_users($editor_ids);

This code deletes all users with the ‘editor’ role.

Deleting Users with No Posts

$args = array('has_published_posts' => false); 
$users_no_posts = get_users($args); 
$users_no_posts_ids = array();

foreach ($users_no_posts as $user) { 
  $users_no_posts_ids[] = $user->ID; 
}

confirm_delete_users($users_no_posts_ids);

This code deletes all users who have not published any posts.

Deleting Users Registered More Than a Year Ago

$args = array('date_query' => array('before' => '1 year ago')); 
$old_users = get_users($args); 
$old_user_ids = array();

foreach ($old_users as $user) { 
  $old_user_ids[] = $user->ID; 
}

confirm_delete_users($old_user_ids);

This code deletes all users who registered more than a year ago.

Deleting Users from a Specific Blog in a Multisite Network

$args = array('blog_id' => 2); 
$blog_users = get_users($args); 
$blog_user_ids = array();

foreach ($blog_users as $user) { 
  $blog_user_ids[] = $user->ID; 
}

confirm_delete_users($blog_user_ids);

This code deletes all users from the blog with ID 2 in a multisite network.