WordPress: Changing the text above the password on a password protected page

by | Sep 16, 2014 | Wordpress | 0 comments

If you password protect a page or post in WordPress, by changing the ‘visibility’ to “Password Protected”, WordPress will present the user with a statement, “This post is password protected. To view it please enter your password below:” and a form for the user to enter and submit their password.

In my case, I wanted to give the user a free password, but I wanted them to supply their contact information first. With the default behavior of WordPress, the user has no idea that they can get a password for free or how to go about it. Since I already had a page set up to give the user a password, all I needed to do was direct them to the proper page to get the password.

There are some paid plugins that will let you modify the content of a password protected page. Some even allow you to password protect a part of the page. I didn’t need to do anything that fancy. I found the answer in the WordPress Codex.

It’s simply a matter of adding a new function to the functions.php file in your child theme (or creating a new functions.php file there if it doesn’t already exist).  The function is this:

function my_password_form() {

global $post;

$label = ‘pwbox-‘.( empty( $post->ID ) ? rand() : $post->ID );

$o = ‘<form action=”‘ . esc_url( site_url( ‘wp-login.php?action=postpass’, ‘login_post’ ) ) . ‘” method=”post”> ‘ . __( “<p>Put the content you want to appear above the password entry box here.  <a href=\”http://globalcreations.com\”>A link would look like this</a>.</p>” ) . ‘ <label for=”‘ . $label . ‘”>’ . __( “Password:” ) . ‘ </label><input name=”post_password” id=”‘ . $label . ‘” type=”password” size=”20″ maxlength=”20″ /><input type=”submit” name=”Submit” value=”‘ . esc_attr__( “Submit” ) . ‘” /> </form> ‘;

return $o;

}

add_filter( ‘the_password_form’, ‘my_password_form’ );

Here are some points to remember:

  • The above function must be between the <?php  and ?> tags in functions.php.  If this is the first function you are adding to your child theme, be sure to add those tags.
  • Notice in the above where it says “<p> Put the content . . . ” .  That’s where you put whatever content you want to appear above the password box.
  •  You can add html to the content, but beware that certain punctuation, like ” must be escaped like this \” so the code doesn’t get confused. See the example link provided above.
  • Be sure there is no blank line at the bottom or top of your functions.php file.  Strange things can happen if there is.

That’s all there is to it.