Wordpress- Delete user programatically

How can we delete user programatically in wordpress?

Solution:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
global $wpdb;
$curr_date= date('Ymd');
 
require_once(ABSPATH.'wp-admin/includes/user.php' );
$users = get_users(array(
    'meta_key'     => 'user_delete_date',
    'meta_value'  => "$curr_date",
));
 
foreach($users as $usr){
    $user_id= $usr->ID;
    $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>';
      }
    
    }
}

No comments:

Post a Comment