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; }