WordPress.org

Ready to get started?Download WordPress

Forums

Howto add an error message to password protected pages (3 posts)

  1. lanclet11
    Member
    Posted 10 months ago #

    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.

  2. KrissieV
    Member
    Posted 10 months ago #

    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>
    	</form>';
    
           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;
    } ?>
  3. Bob Varaleau
    Member
    Posted 3 months ago #

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

Reply

You must log in to post.

About this Topic