Wordpress- Delete user programatically

How can we delete user programatically in wordpress?

Solution:

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

No comments:

Post a Comment