Set Session Time Limit for Password Protected Posts

I have made a series of posts and pages password protected on my website using the built-in Visibility setting. They all use the same password, so the user can view all of them after entering the password once.

The problem: I need the session to time out after an hour. I have tried using this code in my functions.php file.

add_action( 'wp', 'post_pw_sess_expire' );
    function post_pw_sess_expire() {
    if ( isset( $_COOKIE['wp-postpass_' . COOKIEHASH] ) )
    // Setting a time of 0 in setcookie() forces the cookie to expire with the session
    setcookie('wp-postpass_' . COOKIEHASH, '', 0, COOKIEPATH);
}

It successfully clears the cache, but I would like to extend the time rather than expiring the session immediately. Any help would be greatly appreciated!

2 Answers
2

The reason is when you execute this code

setcookie('wp-postpass_' . COOKIEHASH, '', 0, COOKIEPATH);

It will reset your post password cookie to blank ”, so it just work once To solve this you need to assign the original cookie and extend the timeout, like this

setcookie('wp-postpass_' . COOKIEHASH, $_COOKIE['wp-postpass_' . COOKIEHASH], time() + 60 * 5, COOKIEPATH);

Hope this help

Leave a Comment