Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

Check user last login date

I 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 hook
  • after_password_reset action hook
  • login_redirect filter hook
  • update_user_meta()
  • get_user_meta()

Related Posts:

  1. Prevent users from display default wordpress login form
  2. login redirect based on user role not work as expected
  3. User registration followed by automatic login
  4. Include WP_Query in my own PHP file?
  5. When to use Exceptions vs Error Objects vs just plain false/null
  6. Adding “Remember Me” in custom login
  7. How to change the wp-login.php page title?
  8. WordPress URL Rewrite not working
  9. How to use update and delete query in wordpress
  10. add_meta_boxes action with refresh on save
  11. Resize Image without cropping
  12. how to create and show sessions in word press?
  13. Using a nonce in a Custom Login Form
  14. Constructing a custom login form using ajax
  15. automated tests as a user?
  16. Custom plugin issue when trying to use the shortcode twice on a page [closed]
  17. When is is_admin() available?
  18. New Plugin Review
  19. Create custom blocks for bootstrap
  20. Hiding WordPress Plugin Source Code
  21. Admin username and password
  22. wp_insert_post() is returning the correct post ID, no failure, but the post content does not get updated
  23. wp_loaded hook block script enquequing
  24. Is it necessary to sanitize wp_set_password user input?
  25. Custom filter in admin edit custom post type responding with invalid post type?
  26. WordPress Scheduled Event Request Blocking
  27. How to give new users two specific user role options upon WordPress user registration
  28. How to find error in my code when the error message is pointing to WP core file?
  29. How to access global variable $menu inside a class function
  30. Custom user login page by creating a plugin
  31. Singelton class does not work, multiple initialization on page reload
  32. Saving an array of dynamic repeater data as post_meta
  33. WordPress wpform plugin submit and get multiple checked value from checkbox [closed]
  34. How to upload a file to a folder named after the user_id via plugin
  35. developing a wordpress plugin, have a few PHP Woocommerce related coding questions
  36. Whitelisting items from custom options page
  37. Add_menu_page not displaying the menu in class based plugin
  38. Script to browser problem PHP
  39. adjust section according to country?
  40. wp query foreach deleting record returning only first or last item
  41. PHP > Scheduled Tasks > Sending daily email with dynamic API variables
  42. How can I hide that I Use WordPress (with W3 Total Cache)
  43. wp_insert_post: array only. wp_update_post: array|object (?)
  44. Is it possible to define variables in a wordpress shortcode, and then call the shortcode using a specific variable?
  45. Building a REST API for your web app exposes primary keys of DB records?
  46. How to remove the message ‘We could not find any results for your search’ without changing template files and without adding posts/pages?
  47. Custom meta box values are not getting saved for my custom post type
  48. Custom Registration username_exists / email_exists
  49. How can I search all plugins for composer’s vendor/autoload.php?
  50. Action Hook Inside WordPress Plugin Shortcode
  51. Show login greeting above sub-menu links?
  52. Submit form to db
  53. Add a custom WooCommerce settings tab with sections
  54. PHP using external anonymous function inside class
  55. Using ACF Relationship field to set post type to draft or published status
  56. Log out without confirmation request (nonce)
  57. Add Pre-Defined Value to Click Counter in WordPress
  58. How can i avoid duplicate same post in wp?
  59. Drop down question
  60. code that I can run, or a plug in to show what sql tables something pulls information from
  61. Automatic email message after manual user approval
  62. custom mailchimp form using HTTP API
  63. How to override theme’s public static function inside of a trait?
  64. pass datetime using wp_localize_script to frontend from settings page
  65. Infinite loop when logging out using custom login form
  66. add custom metabox to media library custom widget
  67. using filter and hook inside class
  68. Display attached images of a page or post that are insetred using gallery
  69. overwrite wordpress gallery with custom gallery shortcode
  70. Enqueue sripts and styles only if function is called
  71. Add widget area from visual editor
  72. Register/enqueue scripts only on certain admin pages
  73. Looping through custom data in a custom table to display all items in a post
  74. Issues adding Recaptcha v3 to WordPress Registration
  75. Change Login or Logout text based on status
  76. Redirecting the lost password page request when using a custon login page
  77. How to properly escape in ternary operators – Wp Coding Standards?
  78. not able to access $_POST on backend profile update
  79. Custom Plugin Develoment, Form Action
  80. WordPress Query Crashes Browser
  81. How to send logs to plugin owner for a plugin?
  82. How to Request a User to Register on Landing at a Site, Then Automatically Delete the Users Password on Logout?
  83. Plugin Modification Change Functionality For Logged User Only
  84. Force CSV download with template_redirect
  85. Adding a sidebar to wp-login.php
  86. INCOMING: Wall of code for form and $_POST, not updating custom field’s value
  87. add shortcode heading showing multiple time
  88. WP multisite network plugin fails to see classes loaded with spl autoload
  89. AJAX & PHP | Call a specific PHP function from a PHP file via AJAX?
  90. What is this mark for “? function()” [closed]
  91. using a shortcode in a hyperlink
  92. Transate plugin with js & wp_localize_script
  93. WP Custom tables query
  94. Import js variables loaded via wp_localize_script() into js module without global scope connection
  95. Import users and custom user meta from csv
  96. Create custom table for wordpress custom registration flow
  97. Authenticate + Authorize WP REST API request before built-in WP JSON Schema Payload Validation?
  98. WordPress REST API – Custom field not added to pages
  99. WordPress wp_set_object_terms does not assign product to custom taxonomy
  100. Can’t programmatically log user in php
Categories PHP Tags login, php, plugin-development
How to escape html code?
URL rewrite parameter lost (add_rewrite_rule)

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress