WordPress.org

Ready to get started?Download WordPress

Forums

WP Last Login
Column sort not working (21 posts)

  1. czcsabi
    Member
    Posted 1 year ago #

    Hi,

    I installed the plugin yesterday, and noticed that the column sorting function isn`t working as expected. The list of users is ordered only by the user name instead of last login date. Many of my users have logged in since the install so some of them are having actual dates shown, so theoretically it should be making some sorting.

    Best Regards,
    Csaba Czompo

    http://wordpress.org/extend/plugins/wp-last-login/

  2. RyanThaDude
    Member
    Posted 1 year ago #

    I'm also noticing this issue as well as it's not sorting as expected. It is acting like I'm sorting the names instead of the dates. This was the reason for using this plugin.

  3. Konstantin Obenland
    Theme Wrangler
    Plugin Author

    Posted 1 year ago #

    Do you guys have any other user- or member-oriented plugin installed?

    Thanks,
    Konstantin

  4. czcsabi
    Member
    Posted 1 year ago #

    Yes I have Cimy User Extra Fields.

  5. Konstantin Obenland
    Theme Wrangler
    Plugin Author

    Posted 1 year ago #

    If you deactivate that plugin, does the sorting work then?

  6. czcsabi
    Member
    Posted 1 year ago #

    No. Seemingly it`s not a plugin conflict, because on a smaller site with 4 users (with different login dates) even if I deactivate all plugins (Cimy included), the sort is taking place for the user names.

  7. czcsabi
    Member
    Posted 1 year ago #

    A bit dirtier working alternative I`ve come up with is:

    add_action('pre_user_query','wpse_27518_pre_user_query');
    function wpse_27518_pre_user_query($user_search) {
        global $wpdb,$current_screen;
    
        if ( 'users' != $current_screen->id )
            return;
    
        $vars = $user_search->query_vars;
    
        if('wp-last-login' == $vars['orderby'])
        {
            $user_search->query_from .= " INNER JOIN {$wpdb->usermeta} m1 ON {$wpdb->users}.ID=m1.user_id AND (m1.meta_key='wp-last-login')";
            $user_search->query_orderby = ' ORDER BY UPPER(m1.meta_value) '. $vars['order'];
        }
    }
  8. ds123
    Member
    Posted 1 year ago #

    i'm using buddypress and sorting is not working

  9. ds123
    Member
    Posted 1 year ago #

    i'd like to use this plugin to help me weed out the inactive accounts on my site .... can you please post the mysql query that will query the database and return a list of usernames and date of last login ...sorted from most recent login on down?

  10. czcsabi
    Member
    Posted 1 year ago #

    The code above, when pasted into your functions.php file does the exact same thing ... it makes the login date column sortable.

  11. czcsabi
    Member
    Posted 1 year ago #

    You can even schedule a daily automatic delete of unused accounts, specifying the max. days without login, like so (to be put in functions.php):

    add_action('mydailyclearout', 'deleteinactiveusers');
    
    function my_activation() {
    if( !wp_next_scheduled( 'mydailyclearout' ) ) {
     wp_schedule_event(time(), 'daily', 'mydailyclearout');
     }
    }
    
    add_action('wp', 'my_activation');
    
    function deleteinactiveusers() {
    	  $days = 60; // days without login
    	  $meta_value = time() - ($days * 24 * 60 * 60);
    	  $user_query = new WP_User_Query(
    	    array(
    			'meta_key'       => 'wp-last-login',
    			'meta_value'     => $meta_value,
    			'meta_compare'   => '<',
    			'meta_type'      => 'NUMERIC'
    		    )
    	     );
    
      	  $users = $user_query->results;
         if (!empty($users)){
    	foreach( $users as $user ) {
    	 if ($user->roles[0] =="subscriber"){	// only delete subscribers
              $user_email = stripslashes($user->user_email);
    	  $user_name = $user->user_login;
    	  $headers = array("Content-Type: text/html; charset=UTF-8");
    	  wp_mail($user_email, $user_name . " unused account deleted",'Your unused account <strong>' . $user_name . '</strong> has been deleted.',$headers); // send an email notification
              wp_delete_user($user->ID); // delete the user
    	 }
    	}
           }
    
      $days=50; // you can even send a notification several days before for their consideration
    	  $meta_value1 = time() - ($days * 24 * 60 * 60);
    	  $meta_value2 = time() - (($days+1) * 24 * 60 * 60);
    	  $user_query = new WP_User_Query(
    	    array(
    			'meta_key'       => 'wp-last-login',
    			'meta_value'     => array($meta_value2,$meta_value1),
    			'meta_compare'   => 'BETWEEN',
    			'meta_type'      => 'NUMERIC'
    		    )
    	     );
    
      	  $users = $user_query->results;
         if (!empty($users)){
           foreach( $users as $user ) {
    	 if ($user->roles[0] =="subscriber"){
    	  $user_email = stripslashes($user->user_email);
    	  $user_name = $user->user_login;
    	  $headers = array("Content-Type: text/html; charset=UTF-8");
    	  wp_mail($user_email, "Important! ". $user_name . " account is scheduled to be deleted",'More then ' . $days . ' have passed since your last login with <strong>' . $user_name . '</strong> username. If you dont login in several days your account will be deleted',$headers); // send notification email
    	 }
           }
       }
    }

    I`m using this with success on my site, but for security you should test it out first on a nonproduction site first.
    On a side note, this won`t list/delete those users to have never logged in or only before first plugin activation. For them you have to do the job manually.

  12. czcsabi
    Member
    Posted 1 year ago #

    For the wp_delete_user function to properly work I had to include the user.php file like so:
    require_once( ABSPATH.'wp-admin/includes/user.php' );
    The modified code is:

    add_action('mydailyclearout', 'deleteinactiveusers');
    
    function my_activation() {
    if( !wp_next_scheduled( 'mydailyclearout' ) ) {
     wp_schedule_event(time(), 'daily', 'mydailyclearout');
     }
    }
    
    add_action('wp', 'my_activation');
    
    function deleteinactiveusers() {
    	  $days = 60; // days without login
    	  $meta_value = time() - ($days * 24 * 60 * 60);
    	  $user_query = new WP_User_Query(
    	    array(
    			'meta_key'       => 'wp-last-login',
    			'meta_value'     => $meta_value,
    			'meta_compare'   => '<',
    			'meta_type'      => 'NUMERIC'
    		    )
    	     );
    
      	  $users = $user_query->results;
         if (!empty($users)){
            require_once( ABSPATH.'wp-admin/includes/user.php' ); // this had to be added for the wp_delete_user function to work properly
    	foreach( $users as $user ) {
    	 if ($user->roles[0] =="subscriber"){	// only delete subscribers
              $user_email = stripslashes($user->user_email);
    	  $user_name = $user->user_login;
    	  $headers = array("Content-Type: text/html; charset=UTF-8");
    	  wp_mail($user_email, $user_name . " unused account deleted",'Your unused account <strong>' . $user_name . '</strong> has been deleted.',$headers); // send an email notification
              wp_delete_user($user->ID); // delete the user
    	 }
    	}
           }
    
      $days=50; // you can even send a notification several days before for their consideration
    	  $meta_value1 = time() - ($days * 24 * 60 * 60);
    	  $meta_value2 = time() - (($days+1) * 24 * 60 * 60);
    	  $user_query = new WP_User_Query(
    	    array(
    			'meta_key'       => 'wp-last-login',
    			'meta_value'     => array($meta_value2,$meta_value1),
    			'meta_compare'   => 'BETWEEN',
    			'meta_type'      => 'NUMERIC'
    		    )
    	     );
    
      	  $users = $user_query->results;
         if (!empty($users)){
           foreach( $users as $user ) {
    	 if ($user->roles[0] =="subscriber"){
    	  $user_email = stripslashes($user->user_email);
    	  $user_name = $user->user_login;
    	  $headers = array("Content-Type: text/html; charset=UTF-8");
    	  wp_mail($user_email, "Important! ". $user_name . " account is scheduled to be deleted",'More then ' . $days . ' have passed since your last login with <strong>' . $user_name . '</strong> username. If you dont login in several days your account will be deleted',$headers); // send notification email
    	 }
           }
       }
    }
  13. taras_bulba
    Member
    Posted 11 months ago #

    Column sorting is definitely not working.

    I don't have any other plugin that works with the users data.

    I've tried this: http://wordpress.org/support/topic/admin-column-sorting?replies=14

    and this: http://j3webworks.com/blog/wordpress-add-and-sort-custom-column-in-users-admin-page

    with no luck.

    Whatever I do, it sortb by login (username) no matter what.

    Any idea anyone?

    Thanks.

  14. Nakonama
    Member
    Posted 11 months ago #

    Same problem here, when click last login, it sort by the name,

    how can we solve it ?

    Jack

  15. Nakonama
    Member
    Posted 11 months ago #

    Any update here ?

    will it be possible to sort the last login date ?

    Jack

  16. Eric Langley
    Member
    Posted 10 months ago #

    Column sorting is not working for me as well. BP 1.81 and WP 3.6

  17. taras_bulba
    Member
    Posted 9 months ago #

    Does anyone know how to fix this?

  18. roddyp
    Member
    Posted 9 months ago #

    czcsabi's wpse_27518_pre_user_query addition to function.php makes sorting work fine for me.

  19. taras_bulba
    Member
    Posted 9 months ago #

    How did you implement it exactly?

  20. roddyp
    Member
    Posted 8 months ago #

    I just added the code into functions.php: From the Admin menu, select Appearance/Editor, then Theme Functions (functions.php).

    <?php
    
    add_action('pre_user_query','wpse_27518_pre_user_query');
    
    function wpse_27518_pre_user_query($user_search) {
        global $wpdb,$current_screen;
    
        if ( 'users' != $current_screen->id )
            return;
    
        $vars = $user_search->query_vars;
    
        if('wp-last-login' == $vars['orderby'])
        {
            $user_search->query_from .= " INNER JOIN {$wpdb->usermeta} m1 ON {$wpdb->users}.ID=m1.user_id AND (m1.meta_key='wp-last-login')";
            $user_search->query_orderby = ' ORDER BY UPPER(m1.meta_value) '. $vars['order'];
        }
    }
  21. taras_bulba
    Member
    Posted 8 months ago #

    Strange, it has worked in one of my sites, but not in another.

    It also doesn't sort if you are in a multisite installation and are a looking at wp-admin/network/users.php . It does if your are looking the user list of one site in the network,

    Thanks roddyp

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags

No tags yet.