If you ever need to set up a simple password protected area in WordPress, these snippets and plugins might come in handy when you’re hacking through your theme.

I believe this would be the logical approach: you create a main password protected page for your private section and then you create a bunch of child pages under that page. This way you’ll be using native WordPress functionality (page organization and page visibility) for most of your private section.

But you may find that a couple of patches will be in order.

WordPress Plugins

FT Password Protect Children Pages

One thing you will not want will be to have to password protect each subpage individually. You’ll just want to set a password for the main parent page and have all its child pages inherit the password protection. This plugin does exactly that.

Page Template Extended (2021 edit: not in the wp plugin repo anymore, but you can still find it on GitHub)

Similarly, if you decide to use a specific page template for the protected section, it would be ideal to just set the page template to the main parent page and not have to worry about setting it for each of its child pages. This plugin makes subpages inherit their parents template.

Logout Password Protected Posts

You’ll also need a log out link that WordPress doesn’t provide by default. After installing this plugin you’ll be able to use php do_action(‘posts_logout_link’); ?> anywhere in your WordPress theme to produce the log out link.

Listing the subpages

Use this code in your theme to display the main parent page as a heading and the child pages below it:

<?php
/* List Subpages */
$ancestors = get_post_ancestors($post); 
$top_parent_id = end($ancestors); 
if (!$top_parent_id) $top_parent_id = $post->ID; 
?> 
<h3 <?php if ($top_parent_id == $post->ID ) echo "class='current_page_item'"?> > 
  <a href="<?php echo get_permalink($top_parent_id) ?>" title="<?php echo get_the_title($top_parent_id) ?>"> 
    <?php echo get_the_title($top_parent_id) ?> 
  </a> 
</h3> 
<ul> 
  <?php wp_list_pages('sort_column=menu_order&title_li=&child_of='.$top_parent_id); ?> 
</ul>

If you’ve created a page template for your protected pages, you can use the code above in it to give your users a navigation between the subpages.

A couple of snippets for functions.php

Change the default password protected text

The default text on password protected posts or pages is This post is password protected. To view it please enter your password below. Should you wish to change this text, add the following code to your theme’s functions.php file:

/* Change default password protected text */
add_filter('the_password_form', 'custom_password_form');
function custom_password_form($form) {
 $subs = array(
 '#<p>This post is password protected. To view it please enter your password below:</p>#' => '<p>This is my custom text for the password protected items.</p>',
 '#<form(.*?)>#' => '<form$1 class="passwordform">',
 '#<input(.*?)type="password"(.*?) />#' => '<input$1type="password"$2 class="text" />',
 '#<input(.*?)type="submit"(.*?) />#' => '<input$1type="submit"$2 class="button" />'
 );
 echo preg_replace(array_keys($subs), array_values($subs), $form);
}

Change the part where it says “This is my custom text…” in the code above.

Remember: never edit WordPress core files. Either use a plugin or use the functions.php file and filter your changes in from there.

Remove the word “Private:” from the titles

By default the titles of the password protected pages are preceded by the word “Private:” (followed by a colon). To prevent this from happening, add the following filter to your theme’s functions.php file:

// Removing "Private:" from private pages
add_filter('the_title','remove_private_prefix'); 
function remove_private_prefix($title) { 
  $title = str_replace('Private:','',$title); 
  return $title; 
}