I protected a page with password. Iâd like to add a short error message when the inserted password is incorrect.
How can I do this?
I add this code to show and customize the form on my page.
My functions.php
add_filter( 'the_password_form', 'custom_password_form' );
function custom_password_form() {
global $post;
$label = 'pwbox-'.( empty( $post->ID ) ? rand() : $post->ID );
$o = '<form class="protected-post-form" action="' . get_option('siteurl') . '/wp-pass.php" method="post">' .
'<p class="glossar-form-p">Alle weiteren Glossarbeiträge sind durch ein Passwort geschützt. </p>' .
' <label for="' . $label . '">' . ' </label><input name="post_password" id="' . $label . '" type="password" size="20" />
<input type="submit" name="Submit" value="' . esc_attr__( "Login" ) . '" />
</form>
';
return $o;
}
The latest entered password is stored as a secure hash in a cookie named
'wp-postpass_' . COOKIEHASH
.When the password form is called, that cookie has been validated already by WordPress. So you just have to check if that cookie exists: If it does and the password form is displayed, the password was wrong.
Following up from fuxia‘s answer. The complete snippet, including the check if the page load came from the same page, would be:
Just be sure to use
wp_get_raw_referer()
instead ofwp_get_referer()
as the latter will returnfalse
in case the current page and the referrer page are the same.Maybe it’s really really late to answer. Something you need to do the following. As there is no default way to validate you need to follow few steps. Here i gonna use session variable to check matching the generated cookies. first need to start session.
Then use the following code where you want to show the error msg.
That’s it!!