2

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?

2
  • Are you trying to delete a user, if he is not admin?? Commented Jul 17, 2016 at 10:09
  • Yes. I am trying to delete a user, if he is not admin. Commented Jul 17, 2016 at 10:15

6 Answers 6

3

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']);
Sign up to request clarification or add additional context in comments.

Comments

2

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.";
    }
}

Comments

0

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.

Comments

0

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

Comments

0

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!

Comments

0

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' );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.