WordPress Mug

We earlier discussed the manual way by which one can use password protection for WordPress posts, but one can also do the same by using the snippets, the short codes. The following article we will discuss all those snippets which can help you in keeping your posts or pages password protected.

Protecting Custom Fields

Unless and until the correct password is entered, WordPress does not show or display a password-protected post’s Content or Excerpt. But, you do also notice that the post’s Custom Field(s) data is not protected and can still be shown.

In order to make it stopped from displaying, you need to wrap your single.php or page.php files get_post_meta calls with adding a conditional statement using: post_password_required. You can also use the post_password_required function within in an if statement to do other customization.

Customized The Protected Text

Using short codes will also give you freedom to change the default text in the password form. You can change the default text for the password-protected post Excerpt by inserting the password form into the post Excerpt by using the following code in your WordPress functions.php file.

The password form basically displays the default message: “This post is password protected. To view it please enter your password below:” Using the snippet will return the form with different text and different html structure. Here do make sure that you don’t use print or echo function at all while using the snippet.

Protected Excerpt Text

Using the following short code will change the password excerpt text. The default Excerpt of a password-protected post is: “There is no excerpt because this is a protected post.” While after using the code will replace that text with the one you wanted. You are also allowed to use HTML here.

Add The Password Form To The Excerpt

You can also print out the password form instead of the Excerpt message. Using the following snippet will let you make the password form to be the Excerpt for a password protected post.

Hiding Password Protected Posts

You do often want to have your password protected posts to show up on different places in your site. You can use the following snippet in your theme’s functions.php file to hide the password protected posts from these pages without affecting your set pagination structure.

Photo Credit: Flickr/Carlos Varela