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');
3
Answers
Thanks for the clarification. It looks like you’re using the wrong function.
get_the_author_meta()
Retrieves the requested data of the author of the current post, so all the users are going to show the date of the author of the post if you use that function.So in your case you need to use
get_user_meta()
You can find info here:
https://developer.wordpress.org/reference/functions/get_user_meta/
Try the solution, if it doesn’t work we will dig further.
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.
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.phpSorry about the bad English
I’ve been trying this on april 14 2022. It displays march 30 2022