Support » Fixing WordPress » Howto add an error message to password protected pages

  • Does anyone know of a way to have WordPress display an error message (or any other type of feedback) for the password form on a password-protected post or page?

    As it stands, marked a post/page as “private” creates the need for a visitor to enter a password to view the content.

    If the visitor enters the incorrect password, nothing happens.
    I’d like to make it so an error message of some kind is displayed, such as “Error: you have entered an incorrect password. Please try again.”

    I have search Google thoroughly and this issue has been around for 3 years with no solutions?! That’s unbelievable. Please help.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Here is some code I modified but haven’t tested pulled from something a little more complicated I had on one of my sites. Just add it to your functions.php file. And adjust the wording as you’d like.

    <?php add_filter( 'the_password_form', 'custom_password_form' );
    function custom_password_form() {
    	global $post;
    	$label = 'pwbox-'.(empty($post->ID) ? rand() : $post->ID);
    	$output = '<form action="' . get_option('siteurl') . '/wp-pass.php" method="post">
    	<p>' . __("This section of the site is password protected. To view it please enter your password below:") . '</p>
    	<p><label for="' . $label . '">' . __("Password:") . ' <input name="post_password" id="' . $label . '" type="password" size="20" /></label> <input type="submit" name="Submit" value="' . esc_attr__("Submit") . '" /></p>
           if (isset($_COOKIE['wp-postpass_' . COOKIEHASH])
     and $_COOKIE['wp-postpass_' . COOKIEHASH] != $post->post_password){ ?>
       		<p style="color:#C00;">Password Invalid, please try again.</p>
    <?php } else { ?>
     <?php } ?>
    	<?php return $output;
    } ?>

    Has any tried this in Twenty Fourteen 3.8.1 ????

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Howto add an error message to password protected pages’ is closed to new replies.