Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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!

share|improve this question
    
I think what you need is the setcookie() manual. –  cybmeta Jun 11 at 18:20
    
Thank you! I have not used setcookie() before, but I will give it a try. –  Susan Jun 11 at 18:23
1  
WoW. Why do you say that you have not use setcookie() before if you are using setcookie()it the code you posted. –  cybmeta Jun 11 at 18:37
    
Fair point. That is a code snippet copied from another source. It does not appear to be working as I would like, and I am unfamiliar with the setcookie() function and how to manipulate it. Thank you for the link to the manual. Hopefully that will help! –  Susan Jun 11 at 18:53

1 Answer 1

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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