WordPress.org

Support

Support » How-To and Troubleshooting » How do I change Password Protected text?

How do I change Password Protected text?

  • Hi,

    When trying to access a password protected page this text is displayed.

    “This post is password protected. To view it please enter your password below:”

    I would like to change this text to let people know how they can obtain a password. Where do I make these changes. I don’t see it in the theme files.

    Thanks in advance for help on this!

Viewing 15 replies - 1 through 15 (of 24 total)
  • This piece of code should do it for you. Place this code in your theme’s functions.php file. You can add customizations to the custom_password_form() function – just don’t use print or echo – the function must return a value.

    <?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">
    	' . __( "This post is password protected. To view it please enter your password below:" ) . '
    	<label for="' . $label . '">' . __( "Password:" ) . ' </label><input name="post_password" id="' . $label . '" type="password" size="20" /><input type="submit" name="Submit" value="' . esc_attr__( "Submit" ) . '" />
    	</form>
    	';
    	return $o;
    }
    ?>

    Thanks, mfields, this worked!

    I’d like to style this function.. any idea how to do that? I usually work on the front end, so I am not very well-versed on php.

    @curvaciousb: Notice that in the function I added a class to the form. You can apply styles to .protected-post-form in your stylesheet.

    hi could you please explain this a bit more i added your code to the end of the funtions.php form and when i load it i get a fatel error so i have to change it back.

    could anyone let me know where i am going wrong

    thank you

    @asbo6544
    1. What fatal error?
    2. Did you accidentally nest the php tags? <?php ?>.

    what i did was just add it to the end of the function.php form and removed the <?php from the start of your code.

    this is the error code i get.

    Fatal error: Call to undefined function esc_attr__() in /mounted-storage/home61b/sub001/sc31192-CUDB/doggingukcontacts.com/wp-content/themes/Tech/functions.php on line 172

    afterwards i also tried to remove ?> from the end of the code and also got the same error code.
    is there somewhere where is has to go can it just be added anywhere??

    thank you for your quick reply mfields

    The function esc_attr__() is not defined in you installation of WordPress… which means that you are using a version that is below 2.8.0… which means that your blog is at risk of being compromised by that new evil WordPress worm thing.

    Please upgrade ASAP.

    If this is not an option you can always replace esc_attr__() with attribute_escape(), but be warned this this function is deprecated which means that it may not exist in future releases of WordPress core.

    Best to upgrade.

    asbo6544

    @asbo6544

    thank you so much for all your help mfields i only wish there was as many people as you around.

    i mananged to find another file that held what i needed to edit but will also upgrade my version of wordpress.

    once again thank you very much for all help.

    ps so when i upgrade to the newest version of wordpress the code above will work and also allow me to style the form???

    @asbo6544 – Thanks for the kind words 🙂 Yes, when you upgrade WordPress, the code I posted above will work just fine, Best wishes.

    Just the script help I needed for controlling the text and initial view of a protected page. Thank you for posting.

    My php is nube level at best so I’m wondering if it’s possible to remove the
    that’s between the label and the password input? I don’t see this from your code.

    You can alter anything inside the custom_password_form() function. I hope this will illustrate:

    custom_password_form(){
       /* Do what ever you want here */
    }

    Hope this helps 🙂

    Ack sorry I didn’t use the correct formating in my post so the tag I wanted to remove was obscured. Again I don’t know php that well but the code with the nice function you provded:

    <label for="' . $label . '">' . __( "Password:" ) . ' </label><input name="post_password" id="' . $label . '" type="password" size="20" /><input type="submit" name="Submit" value="' . esc_attr__( "Submit" ) . '" />

    Produces output like:

    Password: <br />
    [Password Input]

    I’d like to remove that <br /> but in your code there’s nothing between the </label><input> tags so I don’t understand how the break can be removed. Any help appreciated.

    toyNN, I took a look into this and the <br /> is being inserted by the wpautop() function. This function treats the <input> element as a “block”. Unfortunately, there is no easy way to filter this function.

    The following solution is not the best, but will work.

    add_filter( 'the_content', 'no_block_for_input', 2000 );
    function no_block_for_input( $c ) {
    	return preg_replace( '/<br \/>\s<input/', '<input', $c );
    }

    Please bear in mind that it will remove all <br /> elements from the beginning of inputs posted via the Edit Post Content box which may be undesirable…. best I could do though :)’

    May be someone else knows how to better address this?

    Appreciate your work on this…I guess is just best to punt on it for now and allow the break tag there for the password input.

    The rest of the add_filter() functions you provided has been great – it allows me to add help and contact text on the protected pages so my users can request help with access/pw. That was most important.

    Thanks again, David

Viewing 15 replies - 1 through 15 (of 24 total)
  • The topic ‘How do I change Password Protected text?’ is closed to new replies.
Skip to toolbar