Extending auth_cookie_expiration based on user role

I’m trying to extend the lifespan of the authentication cookie, but only for administrators.

My code in functions.php is:

Read More
add_filter( 'auth_cookie_expiration', 'change_admin_cookie_logout' );
function change_admin_cookie_logout( $expirein ) {
    if ($user_id == current_user_can('promote_users')) {
      return 60;  // yes, I know this is 1 minute
    } return 20;
}

The problem I’m having is twofold:

  1. When I leave off the else statement, then login fails. In other words, I seem to have to define the expiration time for admins & non-admins, rather than singularly modifying the admin expiration time.

  2. the above formula ignores “remember me” and applies it globally. I’d like it to apply only when “Remember Me” is checked.

I’ve tried tweaking wp_set_auth_cookie but hit some walls and came back to this method. Any help is greatly appreciated!

Related posts

1 comment

  1. Untested, but the following should only change the expiration time for admins who select ‘remember me’.

    function wpse108399_change_cookie_logout( $expiration, $user_id, $remember ){
        if( $remember && user_can( $user_id, 'manage_options' ) ){
            $expiration = 60;// yes, I know this is 1 minute
        }
        return $expiration;
    }
    add_filter( 'auth_cookie_expiration','wpse108399_change_cookie_logout', 10, 3 );
    

Comments are closed.