• Resolved Bri

    (@tinnyfusion)


    Hi, I would like to suggest a new feature as I have not seen Last Login presented like this anywhere else before. Is it needed? Probably not. Is it cool? Well I think so but then I like little things like this.

    This adds the users last login date and time to their account menu over on the top right of the menu bar within the WordPress administration screen.

    The date format is currently hard coded to jS F, Y \a\t g:ia which will show the following: Last login: 25th March, 2024 at 7:54pm

    // Add last login time to WordPress admin bar menu
    function get_last_login() {
        $user_id = get_current_user_id();
        $last_login = get_user_meta($user_id, 'last_login', true);
        if ($last_login) {
            return date('jS F, Y \a\t g:ia', strtotime($last_login));
        }
        return ''; // No need to display error if 'Never logged in' as only logged in user can see the info
    }
    
    function add_last_login_to_admin_bar_menu($wp_admin_bar) {
        if (is_user_logged_in()) {
            $last_login = get_last_login();
            $edit_profile_node = $wp_admin_bar->get_node('edit-profile');
            if ($edit_profile_node) {
                $new_title = sprintf('Last login: %s', $last_login);
                $wp_admin_bar->add_node(array(
                    'id' => 'edit-profile',
                    'title' => $new_title,
                    'href' => admin_url('users.php'),
                    'parent' => $edit_profile_node->parent,
                ));
            }
        }
    }
    
    add_action('admin_bar_menu', 'add_last_login_to_admin_bar_menu', 999);

    I got the idea from the feature to add users last login date and time to the users.php list. My suggestion obviously isn’t as useful as seeing when other users logged in but it does let the currently logged in user see at a glance how long it has been since they last logged in.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Bowo

    (@qriouslad)

    Thanks for the suggestion and snippet. This does not look like the type of info that many users will find useful to have on the admin bar. What is your use case for having this info shown there?

    Thread Starter Bri

    (@tinnyfusion)

    It was just a proof of concept really and a means for me to push myself to see if I could do it more than anything.

    I just wanted to try something that I’d never seen anyone else do.

    As far as use case, well aside from letting the current user know when they last logged in at a glance there isn’t any that I can think off.

    As I said, it was just a challenge to myself so thought I’d share in case anyone else wanted to have the same thing/see how to add custom stuff to that menu.

    Plugin Author Bowo

    (@qriouslad)

    Got it. Thanks for sharing that. I know the fun of challenging one’s self to try and code something up. ??

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Feature Suggestion: Add last login time to WordPress admin bar menu’ is closed to new replies.