Search code examples
phpwordpressauthenticationshortcode

Wordpress - User last login date shortcode not work


I have this shortcode, it should show the last login date to users. The problem I find is that all other users are viewing my last login date and not their date.

Am I doing something wrong with the shortcode?

// Last Login Shortcode
function user_last_login( $user_login, $user ) {
update_user_meta( $user->ID, 'last_login', date( current_time( 'timestamp' )) );
}
add_action( 'wp_login', 'user_last_login', 10, 2 );

function lastlogin() {  
$last_login = get_the_author_meta('last_login');
$the_login_date = date('F j, Y, g:i a', $last_login);
return $the_login_date;
}
add_shortcode('lastlogin','lastlogin');

Solution

  • I found a solution by myself. I post the code for anyone who will be in the same problem as me. Also I extended the code, now you get both the current date of login and the date of last access before the current login.

    I’m not very good with these things, I’m relatively new in php, wordpress and codes in general. I invite anyone to clean the code and make it shorter and easier if possible.

    Put the code below in the functions.php file of your child theme.

    // Function that set last login
    add_action('wp_login', 'set_last_login', 0, 2);
    function set_last_login($login, $user) {
        $user = get_user_by('login',$login);
        $time = current_time( 'timestamp' );
        $last_login = get_user_meta( $user->ID, '_last_login', 'true' );
        if(!$last_login) {
        update_user_meta( $user->ID, '_last_login', $time );
        } else {
        update_user_meta( $user->ID, '_last_login_prev', $last_login );
        update_user_meta( $user->ID, '_last_login', $time );
        }
    }
    
    // Function that get last login
    function get_last_login($user_id, $prev = null) {
        $last_login = get_user_meta($user_id);
        $time = current_time( 'timestamp' );
        if(isset($last_login['_last_login_prev'][0]) && $prev) {
            $last_login = get_user_meta($user_id, '_last_login_prev', 'true' );
        } else if(isset($last_login['_last_login'][0])){
            $last_login = get_user_meta($user_id, '_last_login', 'true' );
        } else {
            update_user_meta( $user_id, '_last_login', $time );
            $last_login = $last_login['_last_login'][0];
        }
        return $last_login;
    }
    
    // Shortcode 1
    function last_login_date() {
        global $current_user;
        echo '<p>Last login date: '. date("j M Y - H:i", get_last_login ($current_user->ID, true)) . '</p>';
    }
    add_shortcode('lastlogin', 'last_login_date');
    
    // Shortcode 2
    function current_login_date() {
        global $current_user;
        echo '<p>Current: Login date: '. date("j M Y - H:i", get_last_login($current_user->ID)). '</p>';
    }
    add_shortcode('currentlogin', 'current_login_date');
    

    1. Use [lastlogin] if you want to show the last login date (not the current one)

    2. Use [currentlogin] if you want to show the current date with each new login.

    How to change date and time format: You can make the change to both shortcode 1 and shortcode 2. Edit "j M Y - H:i" as you like, here’s some useful info https://www.php.net/manual/en/datetime.format.php

    Sorry about the bad English