post_password_required() not recognizing cookie set with correct password

Using the below code:

<?php if ( post_password_required() ) { ?>
    <form method="post" action="/wp-pass.php">
        <p>This content is password protected. To view it please enter your password below:</p>
        <input type="password" style="margin:10px 0;" size="20" id="pwbox-<?php the_ID(); ?>" name="post_password"/></label><br/>
        <input type="submit" value="Submit" name="Submit"/></p>
    </form>
<?php } else { ?>
    // echo out all of the post content
<?php } ?>

worked to hide specific content behind the WordPress password protected functionality.

However, this has stopped working. The cookie is still being set and I can see it in the browser settings, but the function has stopped recognizing that the correct cookie is present and just reloads the page asking for the password again.

I don’t use this functionality a lot, so it’s hard to say wehen it stopped working exactly, but it could be related to upgrading WordPress to the most recent release. Would that make sense?

UPDATE:

Here’s the exact cookie content:

Name:   wp-postpass_xxxxxxxxxxxxxxxxxxxxx
Content:    secret
Domain: www.domain.com
Path:   /
Send for:   Any kind of connection
Accessible to script:   Yes
Created:    Monday, June 25, 2012 11:55:26 AM
Expires:    Thursday, July 5, 2012 11:55:25 AM

As you can see, the cookie contains the correct password content but the function has stopped recognizing this as being correct or valid.

UPDATE #2

Removed the testing URL now that the solution has been posted.

1 Answer
1

Figured it out!

The new version of WordPress (3.4) changed the way the password protected pages worked.

This should work for you now:

    <?php if ( post_password_required() ) { ?>
    <form method="post" action="/wp-login.php?action=postpass">
        <p>This content is password protected. To view it please enter your password below:</p>
        <input type="password" style="margin:10px 0;" size="20" id="pwbox-<?php the_ID(); ?>" name="post_password"/></label><br/>
        <input type="submit" value="Submit" name="Submit"/></p>
    </form>
<?php } else { ?>
    // echo out all of the post content
<?php } ?>

I changed your line “/wp-pass.php” to the new “wp-login.php?action=postpass” way.

Leave a Comment