Reputation: 1537
In Wordpress, How can I delete a user pro grammatically if I have a user ID?
I am using below code.
$user_id = 4;
$roles = array();
$user = get_userdata($user_id);
$capabilities = $user->{$wpdb->prefix . 'capabilities'};
if (!isset($wp_roles))
$wp_roles = new WP_Roles();
foreach ($wp_roles->role_names as $role => $name) :
if (array_key_exists($role, $capabilities))
$roles[] = $role;
endforeach;
if (!in_array("administrator", $roles)) {
if (wp_delete_user($user_id)) {
echo 'User deleted' . $user_id;
echo '<br>';
}
}
It is not working for me. Please help me where am I wrong?
Upvotes: 2
Views: 10922
Reputation: 7
You can use the following code:
function my_delete_user( $user_id ) {
global $wpdb;
$user_obj = get_userdata( $user_id );
$id_user = $user_obj->ID;
$idsss = $user_obj->user_id;
/*Delete Data from friend table*/
$query_friend= $wpdb->query("DELETE FROM `wp_user_friends` where `friend_id` = ".$user_obj->ID."");
/*delete data from group*/
$delete_group_table =$wpdb->query("DELETE FROM `wp_group` where `user_id` = ".$user_obj->ID."");
}
add_action( 'delete_user', 'my_delete_user' );
Upvotes: 0
Reputation: 690
This is what I use to delete user along with the metadata.
global $wpdb;
$user_id = "123"; // User id is 123
// Delete User metadata
$wpdb->delete($wpdb->usermeta, ['user_id' => $user_id], ['%d']);
// Delete User
$wpdb->delete($wpdb->users, ['ID' => $user_id], ['%d']);
Upvotes: 3
Reputation: 2283
For WordPress multisite, you can remove a user quite simply from the SQL command line.
In the example below, the user_id is 838 and the site # is 20:
delete from wp_usermeta where user_id = 838 and meta_key in ('wp_20_capabilities', 'wp_20_user_level');
That's it!
Upvotes: 0
Reputation: 4244
You could try the following
global $wpdb;
$ids = $wpdb->get_col('SELECT `user_id` FROM `' . $wpdb->prefix . 'usermeta` WHERE `meta_key` = \'wp_user_level\' AND `meta_value` < 8;');
if (count($ids) > 0)
{
foreach ($ids as $id)
{
if (wp_delete_user($id))
{
echo 'User deleted' . $id;
echo '<br>';
}
}
}
Use this table as reference for user levels
Upvotes: 0
Reputation: 2755
Try this
$user_id = 1;
$user_info = get_userdata( $user_id );
$this_user_roles = $user_info->roles;
//For wp_delete_user() function
require_once(ABSPATH.'wp-admin/includes/user.php' );
if( in_array( "administrator", $this_user_roles) ) {
echo "This user is admin, cannot be deleted";
} else {
if( wp_delete_user( $user_id ) ){
echo "Success user deleted :)";
} else {
echo "There is a problem while deleting the user.";
}
}
Upvotes: 2
Reputation: 1537
I have found the solution to resolve my issue.I have just added a line in code. Now updated code as given below.
require_once(ABSPATH.'wp-admin/includes/user.php' );
$user_id = 4;
$roles = array();
$user = get_userdata($user_id);
$capabilities = $user->{$wpdb->prefix . 'capabilities'};
if (!isset($wp_roles))
$wp_roles = new WP_Roles();
foreach ($wp_roles->role_names as $role => $name) :
if (array_key_exists($role, $capabilities))
$roles[] = $role;
endforeach;
if (!in_array("administrator", $roles)) {
if (wp_delete_user($user_id)) {
echo 'User deleted' . $user_id;
echo '<br>';
}
}
It is now working for me.
Upvotes: 0