admin管理员组文章数量:1188831
I need to show to the user a password change page at first login and ever month. Is there any hook or filter that can I use to check in my plugin code when the user have done last login?
I need to show to the user a password change page at first login and ever month. Is there any hook or filter that can I use to check in my plugin code when the user have done last login?
Share Improve this question asked Oct 1, 2022 at 16:01 devdevdevdev 13 bronze badges 2 |1 Answer
Reset to default 1I think you're more interested in the last time the user reset their password than the last time they logged in. I've written this to check on the last password reset.
add_action( 'wp_login', 'wpse410045_check_last_login', 10, 2 );
/**
* Checks and updates the user's last login time.
*
* @param string $user_login The username.
* @param WP_User $user The user object.
*/
function wpse410045_check_last_login( $user_login, $user ) {
$now = time();
$single = true;
$last_reset = get_user_meta( $user->ID, 'my_prefix_last_password_reset', $single );
// If the user meta hasn't been set, use 0.
if ( empty( $last_reset ) ) {
$last_reset = 0;
}
if ( $now > ( absint( $last_reset + MONTH_IN_SECONDS ) ) {
// However you choose to do this.
require_user_password_reset();
}
}
add_action( 'after_password_reset', 'wpse_410045_update_password_meta', 10 );
/**
* Updates the time of the password reset for a user.
*
* @param WP_User $user The user whose password has been reset.
*/
function wpse_410045_update_password_meta( $user ) {
// Updates the "last password reset" meta to the present time.
update_user_meta( $user->ID, 'my_prefix_last_password_reset', time() );
}
Update
From the comments below: If you're using the login_redirect
filter, you should be able to check for the last password change and redirect the user if their password has "expired".
Replace the first part of the answer above with something like this (I'm assuming you've got a URL where you'll want to redirect your users to actually change their passwords; I use /my-password-change
in the code.)
add_filter( 'login_redirect', 'wpse410045_check_password_age', 10, 3 );
/**
* Checks the user's last password change. Redirects if it's over a month old.
*
* @param string $redirect The destination URL.
* @param string $requested_redirect The requested destination URL.
* @param WP_User $user The user object.
*/
function wpse410045_check_last_login( $redirect, $requested_redirect, $user ) {
if ( is_wp_error( $user ) ) {
// Something went wrong, bail out.
return $redirect;
}
$now = time();
$single = true;
$last_reset = get_user_meta( $user->ID, 'my_prefix_last_password_reset', $single );
// If the user meta hasn't been set, use 0.
if ( empty( $last_reset ) ) {
$last_reset = 0;
}
if ( $now > ( absint( $last_reset + MONTH_IN_SECONDS ) ) {
// Redirect to the password change URL.
return '/my-password-change';
}
// Otherwise, carry on.
return $redirect;
}
The after_password_reset
action should still be fine.
References
wp_login
action hookafter_password_reset
action hooklogin_redirect
filter hookupdate_user_meta()
get_user_meta()
本文标签: phpCheck user last login date
版权声明:本文标题:php - Check user last login date 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738399633a2084704.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
wp_login
hook to add/check/update user metadata indicating the most recent login time (usingadd_user_meta()
/get_user_meta()
/update_user_meta()
). – Pat J Commented Oct 1, 2022 at 16:29wp_login
hook to check this, I will consider to do also the first login check after the account is activated. If you leave an answer with an example I will mark it as accepted – devdev Commented Oct 1, 2022 at 17:00