Forums

How do I change Password Protected text? (16 posts)

  1. gswaim
    Member
    Posted 1 month ago #

    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!

  2. mfields
    Member
    Posted 1 month ago #

    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;
    }
    ?>
  3. gswaim
    Member
    Posted 1 month ago #

    Thanks, mfields, this worked!

  4. curvaciousb
    Member
    Posted 3 weeks ago #

    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.

  5. mfields
    Member
    Posted 3 weeks ago #

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

  6. asbo6544
    Member
    Posted 3 weeks ago #

    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

  7. mfields
    Member
    Posted 2 weeks ago #

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

  8. asbo6544
    Member
    Posted 2 weeks ago #

    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

  9. mfields
    Member
    Posted 2 weeks ago #

    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.

  10. asbo6544
    Member
    Posted 2 weeks ago #

    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???

  11. mfields
    Member
    Posted 2 weeks ago #

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

  12. toyNN
    Member
    Posted 1 week ago #

    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.

  13. mfields
    Member
    Posted 1 week ago #

    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 :)

  14. toyNN
    Member
    Posted 1 week ago #

    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.

  15. mfields
    Member
    Posted 1 week ago #

    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?

  16. toyNN
    Member
    Posted 4 days ago #

    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

Reply

You must log in to post.

About this Topic

Tags

No tags yet.