I want to expire session after 2 hours and also want to expire session when browser closed

add_filter('auth_cookie_expiration', 'my_expiration_filter', 99, 3);
function my_expiration_filter($seconds, $user_id, $remember){
    $expiration = 2*60*60; //UPDATE HERE;
    return $expiration;
  • 1
    If you set the expiration to 2 hours, then the expiration is 2 hours. The only way to end a session when the browser is closed is to set the cookie expiration to 0. You can’t do both. Aug 26, 2019 at 12:08
  • @JacobPeattie How to expire cookie using wordpress
    – Sudhir
    Aug 27, 2019 at 9:32

1 Answer 1


It is the correct hook. I often also add a bit of javascript, to active the checkboxes inside the form. In your case it would not be necessary to use the $accepted_args parameter – 3 – of the add_filter() hook, because you're not using any parameters, and simply work with a return value as integer, in seconds.

if ( ! function_exists( 'keep_me_logged_in_for_1_year' ) ) {

    add_filter( 'auth_cookie_expiration', 'keep_me_logged_in_for_1_year' );
    function keep_me_logged_in_for_1_year() {

        return 31556926; // 1 year in seconds

    add_filter( 'login_footer', 'set_default_true_on_checkbox' );
    function set_default_true_on_checkbox() {
        <script type="text/javascript">
        document.getElementById( 'rememberme' ).checked = true;
        document.getElementById( 'wp-submit' ).focus();


If you also want to check for a closed browser, then you need a check via javascript, that js should also be added via the hook login_footer. See this how to to learn more about the idea behind it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.