0

I have a custom coded page (using a page-xxx.php template). This page defined in the /Pages area of the WP admin has password protection.

The content in my custom template is a form and so that is naturally what you see once past the PW prompt I've setup. The issue is that for some users (not me, I've never been able to recreate the issue) is that when they have completed the form, upon submitting it, instead of reloading the same page with a success msg it reloads the same page but it doesn't seem to remember that they have already got past the password prompt.

Can anyone assist troubleshooting this? I don't know how a password protected WP page is functioning (via session vars/cookies?) but I had assumed that once past it, you could reload it/submit a form on it and you wouldn't need to then get past the PW again.

Can anyone clarify this?

1
  • Your suspicion is correct, a cookie is set when the correct password is entered. It has the name wp-postpass_* where * is the md5 of the password. For those who are being asked for the password again, first check if they see the cookie set. If they do, then perhaps your site is caching the page too aggressively.
    – Brooke.
    Commented Sep 19, 2020 at 18:42

1 Answer 1

5
+50

When a user logs into a protected page, WordPress sets a cookie. We can check for the existence of that cookie with this conditional:

if ( isset( $_COOKIE['wp-postpass_' . COOKIEHASH] ) ) {
  // Do stuff. 
}

Also, you should be aware that only one password is stored in cookies at a time, to the last entry viewed.

By default, the cookie expires 10 days from creation

If you need to change

add_filter('post_password_expires', 'true_change_pass_exp', 10, 1);
function true_change_pass_exp( $exp ){
    return time() + 5 * DAY_IN_SECONDS; // 5 days
}

3
  • Just to check Valerii, it's definitely only 1 extra cookie being created per page?
    – AdamJones
    Commented Sep 19, 2020 at 21:53
  • Only one was last entered in a protected form. developer.wordpress.org/reference/functions/… Commented Sep 19, 2020 at 22:07
  • If @ValeriiVasiliev Suggestion did not work, then maybe check for the page cache.
    – Maidul
    Commented Sep 21, 2020 at 19:08

Your Answer

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

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