3

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!

4
  • I think what you need is the setcookie() manual.
    – cybmeta
    Jun 11, 2014 at 18:20
  • Thank you! I have not used setcookie() before, but I will give it a try.
    – Susan
    Jun 11, 2014 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, 2014 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, 2014 at 18:53

2 Answers 2

3

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

2

You just need to hook into the filter like this and replace 0 with a time calculation, like time() + 60

function set_cookie_expire () {
    echo 0;
}
add_filter('post_password_expires', 'set_cookie_expire');

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.