Thursday, June 23, 2016

Wordpress- Delete users between two registration dates programatically .

function get_users_registered_between_date($start='', $end='' ){
     global $wpdb;
     $prefix =$wpdb->prefix;
     $user_table= $prefix.'users';
     
     if( empty($start) )
          $date = date('Y-m-d');

     if ( empty($end) )
          $end = $start;

     $start_dt = new DateTime($start. ' 00:00:00');
     $s = $start_dt->format('Y-m-d H:i:s');

     $end_dt = new DateTime($end.' 23:59:59');
     $e = $end_dt->format('Y-m-d H:i:s');
   
     $sql = $wpdb->prepare("SELECT ID FROM $user_table WHERE 1=1 AND CAST(user_registered AS DATE) BETWEEN %s AND %s ORDER BY user_login ASC",$s,$e);
     $users = $wpdb->get_results($sql);

     return $users;
}



$start_date='2015-02-14';  // registration date
$end_date='2015-04-17';  // registration date
$users= get_users_registered_between_date($start_date, $end_date ); // users list between date  '2015-02-14' and '2015-04-17'

require_once(ABSPATH.'wp-admin/includes/user.php' );
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>';
      }
    
    } 
}

Friday, June 10, 2016

Wordpress- Delete user programatically

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