Skip to content

Instantly share code, notes, and snippets.

@ipokkel
Last active September 30, 2022 16:07
Show Gist options
  • Save ipokkel/18216639d6ac1e8f3d035426743ade3a to your computer and use it in GitHub Desktop.
Save ipokkel/18216639d6ac1e8f3d035426743ade3a to your computer and use it in GitHub Desktop.
Extend the user's login period if the remember me option was checked at login so users stay logged in for longer periods.
<?php
/**
* This recipe extends the user login period if remember me was checked.
*
* @link https://developer.wordpress.org/reference/hooks/auth_cookie_expiration/
*
* You can add this recipe to your site by creating a custom plugin
* or using the Code Snippets plugin available for free in the WordPress repository.
* Read this companion article for step-by-step directions on either method.
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
function my_user_persistent_login( $expiration, $user_id, $remember ) {
// Adjust login period if user selected remember me.
if ( $remember ) {
// login is valid for a year.
return YEAR_IN_SECONDS; // Can multiply this for multiple years, e.g. YEAR_IN_SECONDS * 2
// return MONTH_IN_SECONDS;
// return DAY_IN_SECONDS;
// return HOUR_IN_SECONDS;
}
// default
return $expiration;
}
add_filter( 'auth_cookie_expiration', 'my_user_persistent_login', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment