Customizing And Styling The Password Protected Form

Customizing And Styling The Password Protected Form

Tutorial Details
  • Difficulty: Intermediate
  • Program: WordPress
  • Version (if applicable): 3.0 +
  • Estimated Completion Time: 30-45 mins.

Final Product What You'll Be Creating

Recently I had a client want one of their WordPress pages protected, no problem! Then he came back and said, can you change the text and make it look better? Hmm… Sure! Here’s how to…


Step 1 Edit Your functions.php File

Alright, let’s open up your functions.php file and add this block of code:

<?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 IS YOUR NEW PASSWORD INTRO TEXT THAT SHOWS ABOVE THE PASSWORD FORM" ) . '
	<label class="pass-label" for="' . $label . '">' . __( "PASSWORD:" ) . ' </label><input name="post_password" id="' . $label . '" type="password" style="background: #ffffff; border:1px solid #999; color:#333333; padding:10px;" size="20" /><input type="submit" name="Submit" class="button" value="' . esc_attr__( "Submit" ) . '" />
	</form><p style="font-size:14px;margin:0px;">∗EXTRA TEXT CAN GO HERE...THIS WILL SHOW BELOW THE FORM</p>
	';
	return $o;
}
?>

Let me dissect this really quick for you. The password-protection code is generated from a file in the wp-includes folder. “Cool”, you think, “I’ll just go in there and make my changes”. Don’t do it! Tempting as it may be, editing the core code is a terrible, lowdown, no good idea. For one reason: What happens if you update WordPress? It gets wiped out…sorry. Okay, so putting this block of code in your functions.php file let’s you amend it and not worry about losing any changes to the form when you update.

As you can see, I actually added classes to the form itself, the label of the form, the password field as well as the button. Now we have everything we need to style it completely with CSS. Oh, and we’re not actually changing anything in the wp-includes folder, so we’re not breaking any rules.


Step 2 Change The Default Password Protect Text

The text in capitals above also shows you what I changed… the first set of text:

THIS IS YOUR NEW PASSWORD INTRO TEXT THAT SHOWS ABOVE THE PASSWORD FORM

As you can see this was your intro text… it used to say this:

This post/page is password protected. To view it please enter your password below

Now we’re able to change it to whatever text you want up there. You can even delete everything between those quotations and just have nothing display up there at all.


Step 3 Change The Password Input Field Label

The default label that’s shown to the left of the input field is Password. Here you can change it to whatever you’d like to say. In my case, I assigned it a class took the label out in css by putting the display to none.

.pass-label { display: none; }

You can also change font size, color, the works to this label


Step 4 Style The Password Input Field

I hate how forms look, but with this addition to the default password input field i’m able to make it look a little more "up to date". Feel free to change it however you see fit.

<input name="post_password" id="' . $label . '" type="password" style="background: #ffffff; border:1px solid #999; color:#333333; padding:10px;" size="20" />

Step 5 Styling The Submit Button

In the functions code, I added a class to the submit button called "button". I did this because I wanted all the buttons on my client’s site to look exactly the same. Uniformity in a site is key. Here’s the CSS I used:

.button {
	background-color: #000;
	color:#fff;
	border: 0;
	font-family: Impact, Arial, sans-serif;
	margin: 0;
	height: 33px;
	padding: 0px 6px 6px 6px;
	font-size: 15px;
	cursor: pointer;
}

For some reason I was getting the arrow when I hovered over the button so I just changed cursor to show pointer and it went back to the hand on hover.


Step 6 Adding Extra Text Below The Form

I actually needed to add a little note to tell people that the password field is case sensitive so to add extra text below the form, I just added a paragraph code and inserted a style tag to it to style the text separately from the rest of the page.

And we’re done!

Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • http://www.xpertdeveloper.com Avi

    Hi Carina,

    Nice post. and one more thing. Your site showing 404 Error Except home page.

    • http://kstudiofx.com Carina Javier
      Author

      Thanks Avi!… Yah I got alerted one I got home from my mini vaca… too bad it was down this whole time! It was a permalinks issue… for everyone’s future information… I had my permalinks set to just postname… make sure not to do that. I’ve heard that your site could go down because of it…. make sure it’s at least category/postname and never JUST postname =)

      But it’s all fixed… feel free to browse my site!

  • http://wpprobusiness.com Adam W. Warner

    Great writeup, very helpful. Ditto to what Avi said above…

  • dj

    I’m sure this is a basic questions; but, you forgot to say just how this “enter password” form actually fits in to the overall scheme of creating and viewing a protected page. This is in the functions page – now how is it implemented. As I say, it’s probably very basic but something that I’m unclear on – and most likely something that many who watch a “tut” like this need to know.

    • http://twitter.com/drale2k Drazen Mokic

      You can activate password protection for any post or page by going to Posts/Pages -> Edit -> Set the “Visibility” property in the right sidebar to “password protected” and enter a password.

      Now when you visit that Post/Page it will ask you for a password before you can view its content.

    • http://kstudiofx.com Carina Javier
      Author

      Thanks Drazen! Yah sorry about that dj… quick thing I overlooked. Let me know if you still have any problems with that.

  • http://kstudiofx.com Carina Javier
    Author

    Also if you’d all like to see a LIVE EXAMPLE, it is for a current client of mine, Mike Donavanik. He’s using it for his bonus features section for his fitness DVD. http://mikedfitness.com/bonus

  • http://www.ransomedesign.co.uk Daniel

    Hi Carina, thanks for the code. A client of mind decided to add password protection to posts at the last minute and the default form is fugly! I would expect there to be a template system where you can make your own form, like searchform.php, but I couldn’t find anything on this! For now this is a great fix, so thanks again.

    I love your work too.

    Dan

  • http://nathangiesbrecht.com Nathan Giesbrecht

    This has been incredibly helpful. Now if only I could figure out a way to make the password’s case insensitive, I’d be set!

  • Barbi

    Thanks a bunch for the helpful information… keep these tutorials coming as they are highly appreciated.

  • enrico

    hi, does not work to me. i have a fatal error.
    where exactly i have to place the new code in functions.php?
    thnx

    • Jenifer

      Posted on Tough questions and help them reivse their plan for the month when they fall short of a goal.I assume you made certain nice points in features also.

  • http://devlog.puredesign.hu Daniel Veres

    Hi,

    There’s no wp-pass.php in new wp versions.

    You need to change the following line:

    to:

    • http://wp.envato.com/ Japh Thomson
      Staff

      Hey Daniel, thanks for the correction. Unfortunately, it looks like your code snippets got eaten :(

      To stop the editor eating your code, be sure to wrap it properly in <pre> tags as directed.

      e.g.
      <pre name=”code” class=”php”>
      // code here
      </pre>

      (Remembering also to replace < with &lt; and > with &gt;)

  • http://www.roseetjosephine.com Brice

    Hi Carina,

    Thanks for the helpful piece of information here. Just wondering if there is any way to center the whole form to fit the screen, instead of having it stand on the left side?! Thanks again

  • http://kstudiofx.com Carina
    Author

    Hey guys, just found out that with the most recent version of wordpress doesn’t use this code anymore. Once I find a workaround I will keep you updated. My clients are using wordpress 3.2 for now. Not sure about 3.3 but know for a fact 3.4 wont work with it anymore.

    • http://www.urbburb.com Stacie

      I found this and it seems to work.

      replace /wp-pass.php with /wp-login.php?action=postpass

      Found the info here…http://tummel.me/wordpress-3-4-custom-password-form/

      • http://www.urbburb.com Stacie

        Oh, and thank you for your code and tutorial! It was exactly what I was looking for!

        Stacie

  • http://www.djvlink.com/forum Dave

    Please let us know when you find a workaround. I just found you page, was so excited to be able to implement this and when I got to the bottom of the comments I learn it doesn’t work lol

    Thank you though for the work you’ve done. I look forward to the update.

  • Robbiegod

    I see this function doesn’t work with latest version of WordPress…Does anyone know how to do this now?

    • Robbiegod

      Ah, helps if i read all of the comments…I followed Stacie’s link and that worked.

    • Ed

      Change ” $o = ‘” to ” $o = ‘”