Password Protect Children Pages

By michael  |  May 23rd, 2009  |  Published in WordPress Plugins  |  57 Comments

This plugin does one thing. If a page that is password protected has children pages, all children pages will be protected with the same password. If the correct password is entered on the parent page or any of its children pages, all related pages will be viewable to the user.

The following logic is invoked whenever the_content or the_excerpt is called from your theme.

  1. Is this a page?
  2. Does this page have an ancestor page
  3. If it does, does the ancestor page require password protection?
  4. If it does, treat this page the same way you would treat the top level ancestor page.

Version 0.2 uses the $post->ancestors property and allows protection of grandchildren pages as well

Download here

Responses

  1. Jens says:

    June 15th, 2009 at 4:28 am (#)

    Hi, I installed your plugin (WP 2.8) but it doesn’t work. Is there an update for the new version of wordpress?

    I would be glad if you can help me, because your small plugin would be the perfect solution for my site :-)

    Thanks a lot!
    Jens

  2. webangela says:

    June 25th, 2009 at 7:36 pm (#)

    how do I emplement this. sorry for a stupid question. it is just what i need. thanks again!

  3. Simon says:

    August 26th, 2009 at 8:20 am (#)

    I’ve implemented a “Members Only Area” on my website where the top page is password-protected, and using this plugin all sub-pages are now automatically protected too.

    Works exactly as I hoped it would, thanks!

  4. Gareth says:

    October 18th, 2009 at 8:17 pm (#)

    Hi there,

    Thanks for the plugin – sadly it only works with child pages not grandchild pages.

    Is there someway this could be added using get_post_ancestors rather than post->parent?

    Cheers,
    Gareth.

  5. Big D says:

    October 22nd, 2009 at 8:38 pm (#)

    I'd like to use this for my website under construction, but a) should I update to newest version of WordPress first? and b) what are some responses to common glitches?

  6. Big D says:

    October 22nd, 2009 at 9:01 pm (#)

    I downloaded this into my website's admin and it doesn't even show up anywhere. What happened? Does it even show up? How can I use it if there's no control panel anywhere?

  7. Glenn says:

    October 23rd, 2009 at 5:43 am (#)

    What kind of interface do you need? I didn't build any options into it. If you password protect a page, its child gets password protected too. Pretty simple.

  8. Trevor Gehman says:

    March 4th, 2010 at 3:10 pm (#)

    In case anyone is interested in making this plugin protect ALL subpages (not just the immediate children), you can edit the plugin file as follows:

    <span class='code' name='php' >

    // This function prints the password form if the parent page is password protected. It is called whenever 'the_content' is invoked.

    function ft_password_protect_children_page_contents( $org_content ){

    if ( is_page() ){

    global $post;

    $ancestors = end($post->ancestors);

    if ( !empty($ancestors) ){

    if ( post_password_required( $ancestors ) ) {

    $real_post = $post;

    $post = $ancestors;

    echo get_the_password_form();

    $post = $real_post;

    return;

    }

    }

    }

    return $org_content;

    }

    add_filter( 'the_content', 'ft_password_protect_children_page_contents' );

    </span>

  9. marie says:

    April 14th, 2010 at 10:14 am (#)

    This is going to sound like the dumbest questions of all, but I'm looking for the way to password protect the PAGES in the first place! After I've figured out how to do that, your plugin will be perfect for what I need.

    Anybody's got any idea how to password protect each page (and possibly their child pages) with a different password?

    A million thanks !

    marie

  10. Glenn Ansley says:

    June 16th, 2010 at 7:34 am (#)

    @marie This is found on the post edit screen in the top-right.

  11. Glenn Ansley says:

    June 16th, 2010 at 11:33 am (#)

    Thanks Trevor. I’ve integrated this with the code and credited you in readme.txt

  12. Scott Walter says:

    July 2nd, 2010 at 9:17 am (#)

    Hi,

    I’ve run into a problem with WordPress 3. I created a test page called “Test1″ and a child page called “Test2″. When I view “Test2″ it puts “PROTECTED” in the name of the page even though “Test1″ nor “Test2″ are marked as protected.

    If i deactivate the plugin then “PROTECTED” goes away from the page title. Any thoughts?

    scott.

  13. Scott Walter says:

    July 2nd, 2010 at 9:22 am (#)

    Another item I just noticed is that pages that I truely have protected show “PROTECTED: PROTECTED:” in the title. Looks like the protected is shown twice for protected pages.

  14. Greg says:

    July 4th, 2010 at 11:32 am (#)

    I just deleted the entire part of the code that adds "protected." I guess that will work for now.

  15. Greg says:

    July 4th, 2010 at 3:06 pm (#)

    Hi Glenn,

    I love your plugin. It is just what I need for my site. I am having a little problem that is coming up though that I thought I would ask you about:

    Since I’ve installed your plugin, whenever I go to _any_ child page on my site the menu changes to prefix _every_ file with “Protected,” even though none of those pages are password protected.

    See here: http://lessonswithgreg.info/students/student-pages/madi/

    Any idea what might be causing this?

  16. Greg says:

    July 4th, 2010 at 10:56 pm (#)

    Ok. That got rid of the “protected”s but none of the children pages are password protected. I deleted then reloaded the original plugin but the children pages are still unprotected. Does this plugin not work with the latest WordPress build?

  17. Drew says:

    July 9th, 2010 at 11:38 am (#)

    Thank you, thank you, thank you. It’s simplicity is perfect for our needs.

  18. Anders Frey says:

    July 9th, 2010 at 3:29 pm (#)

    Hi

    The 0.2 version doesn’t seem to be compatible with WordPress 3.0 (danish). Alle subpages are treated as private pages and the word ‘beskyttet’ (danish for private) is added to all titles. The parent page is not protected.

  19. Mike says:

    August 8th, 2010 at 1:13 pm (#)

    I'm running into an issue after installing this plugin on a site I'm working on. I'd installed this on a different server and had no problems, so I assume this is something about configuration on this new server. But if there's a way around it…

    Here's the error I get now above the page headline on each page:

    Warning: end() [function.end]: Passed variable is not an array or object in /home/content/k/n/s/knscoop/html/test/wp-content/plugins/ft-password-protect-children-pages/ft-password-protect-children-pages.php on line 63

    And then just before the page content (or the password form):

    Warning: end() [function.end]: Passed variable is not an array or object in /home/content/k/n/s/knscoop/html/test/wp-content/plugins/ft-password-protect-children-pages/ft-password-protect-children-pages.php on line 22

    Any idea what could be happening? It's such a great solution for my site, I'd really hate to have to stop using it!

    Thanks!

  20. Julie says:

    August 22nd, 2010 at 10:43 am (#)

    How do you change the look of how it looks on the site? Right now I have a large "This page is password protected" and login button on the top of the page in large type, with the same thing smaller and more typical where it should be in the middle of the page. I'd like to remove it from the top of the page and control the look of the login area/button. How does one adjust the look of the plugin?

  21. Andrew Kaplan says:

    August 25th, 2010 at 4:02 pm (#)

    Plugin works great on my computer, but one person is complaining that on his computer he needs to login to see the “unprotected pages” Any ideas ?

    Great program

    Andrew

  22. Andrew Kaplan says:

    August 26th, 2010 at 9:01 am (#)

    We are having some issues, with some people stating that when they go to non protected areas they are prompted for a password. Thus we had to disable the plugin. Any thoughts would be appreciated.

    Andrew

  23. Joe says:

    September 1st, 2010 at 8:27 am (#)

    Ihave installed and used the plugin. It works fine, although, it puts the word “protected” on some header areas of pages that are not child pages. the word still shows up Even after I delete the orig pass protected pages. Any help would be appreciated.

  24. Merimac says:

    September 1st, 2010 at 8:11 pm (#)

    Hi!
    I don't know if it's me but with something like that this plugin don't work :

    Page 1 (no password)
    -Page 2 (no password)
    –Page 3 (password)
    —Page 4 (no password)
    —-Page 5 (no password)

    With this configuration pages under page 3 didn't get password protection, so I modify your plugin to protect all page under a page with password and it with any location.

    My code is here : http://pastebin.com/mfF4xPym

  25. Philip Bernick says:

    September 21st, 2010 at 1:06 pm (#)

    Hi!

    I like the functionality of this plugin–however, in practice it seems to tag all child pages with "Protected:" in the h1 head, regardless of whether the Parent page was protected or not…

    -p

  26. Glenn Ansley says:

    September 22nd, 2010 at 4:05 am (#)

    0.3 was just released. It fixes all bugs mentioned above this comment.

    == Changelog ==

    = 0.3 =

    * Fixed bug where children pages of non-protected parents had 'Protected: ' prepended to the title.

    * In case where grandparent page is not protected but parent page was, child pages are now protected.

  27. Mike says:

    September 22nd, 2010 at 6:43 am (#)

    I'm going to install 0.3 right now — thanks for your work on this.

    I've got another question which may or may not be something I could configure your plugin to handle: I'd like to display the content of certain blog categories on protected pages. For example, on a protected page I'd have a brief introductory message and then excerpts of three blog posts in the protected category.

    Obviously the content on the pages themselves is the only part that's password-protected at this point and not any blog posts I'm pulling in through the template. So right now the posts show up no matter yet while the page content itself remains protected.

    Is it possible to set up blog posts to be children of protected pages so that they inherit the protection themselves? Or to set up the content rendered by the template to be treated part of the page content? Alternately, do you think I'm barking up the wrong tree and should find a different solution altogether?

    Thanks again for the plugin.

  28. Glenn Ansley says:

    September 22nd, 2010 at 8:26 am (#)

    Hi Mike,
    I just emailed you some code that may accomplish what you're looking to do.

  29. Merimac says:

    September 22nd, 2010 at 5:33 pm (#)

    Thanks for upgrading ;)

  30. joe says:

    September 26th, 2010 at 9:06 pm (#)

    Yes, how do you implement this. You may want to add quick user directions…
    thanks,
    Joe

  31. Glenn Ansley says:

    September 26th, 2010 at 9:08 pm (#)

    Hi Joe,
    You just need to password protect a page like normally in WordPress and then the children pages will be protected as well. When you’re editing a page, this can be done at the top-right by changing the visibility options.

  32. Merimac says:

    October 17th, 2010 at 7:07 pm (#)

    Did you think that possible to use password protection on categories and sub pages ?

  33. Jim says:

    October 25th, 2010 at 9:08 pm (#)

    After installation the following is printed on each page…

    Warning: Invalid argument supplied for foreach() in /home2/rfweb/public_html/W8CBF/wp-content/plugins/ft-password-protect-children-pages/ft-password-protect-children-pages.php on line 72

  34. Mary Carlson says:

    December 29th, 2010 at 7:39 pm (#)

    The wordpress version of this site is not yet published to the hosting site but I have installed the plugin and get the following error on the parent page:

    Warning: Invalid argument supplied for foreach() in /home/content/b/l/a/blackwillowweb/html/hillnhol/wordpress/wp-content/plugins/ft-password-protect-children-pages/ft-password-protect-children-pages.php on line 72

    Warning: Invalid argument supplied for foreach() in /home/content/b/l/a/blackwillowweb/html/hillnhol/wordpress/wp-content/plugins/ft-password-protect-children-pages/ft-password-protect-children-pages.php on line 26

    Can you help?

  35. Christina says:

    February 2nd, 2011 at 11:58 am (#)

    Hi there, I’m looking for the same solutions as Mike. Can you point me in the right direction? You mentioned you emailed something to him.

    Thank you!

  36. Mary Carlson says:

    February 19th, 2011 at 7:28 pm (#)

    I am moving this site over to WP – (not yet published to members) and want to use your plug in but am receiving the following for two children’s pages. Note that I redownloaded v.3 to make sure that the code is the latest.
    Many thanks for your help.

    Warning: Invalid argument supplied for foreach() in /home/content/b/l/a/blackwillowweb/html/hillnhol/wordpress/wp-content/plugins/ft-password-protect-children-pages/ft-password-protect-children-pages.php on line 72

    Warning: Invalid argument supplied for foreach() in /home/content/b/l/a/blackwillowweb/html/hillnhol/wordpress/wp-content/plugins/ft-password-protect-children-pages/ft-password-protect-children-pages.php on line 26

  37. Skyler Moore says:

    March 8th, 2011 at 11:58 am (#)

    While I was working with your plugin I ran into some warnings for lines 26,51, and 71.
    When I casted each of those $ancestors arrays the warnings went away.
    Example
    Change
    foreach ( $ancestors as $ancestor ) {
    to
    foreach ( (array)$ancestors as $ancestor ) {
    This isn’t to much of a concern just noticed some conflicts with another plugin.
    Have a good day.

  38. Wes Morris says:

    June 9th, 2011 at 4:09 am (#)

    Well I’m late to the party and doubt this is tended anymore, but thought I’d take a shot in the dark:

    When a grandchild page is protected with a password different than its parent:

    “Protected: Protected: Discussion Medical Department”

    Is accompanied by a request for the password, but the content DOES display appropriately. So the password functionality seems to work as expected, but the top of the page now indicates protection… squared… and the password entry box is present and functional.

    I’ve been trying to understand the code to figure out how to fix it myself, but haven’t yet and thought I’d ask an expert.

    Thanks for the help if you can offer it. :)

    Wes

  39. Mark says:

    July 29th, 2011 at 10:08 am (#)

    How do I remove the ‘Protected:’ text prefixed to each title?

  40. Amit says:

    August 4th, 2011 at 5:10 am (#)

    Hi Glenn

    Thanks for this unique plugin. Why this functionality is not integrated into WP i beyond me, and I mean for both pages and categories. It doesn’t make any sense to me.

    Anyway, I came across a problem with the plugin which is crucial to weather I can use it. I use the plugin ‘EA attachamnts’ to show the page’s attached files right underneath the content. You can see it in the front page of my site. Now, when password protecting a parent page (through the normal WP process), these attachments are hidden as the rest of the content is. On the other hand, on the child pages, the content is hidden but the attachments are visible. The problem maybe lies in the fact that the appliance of the password protection to the children is not done using the built in WP functionality as one can see that the child pages have not the ‘password protected’ status in the admin as it would be if the password protection would have been done manually. Obviously, the plugin is not operating in database level, but only in runtime. Correct me if I’m wrong.

    This is the only plugin of its kind which I could find. It will be great this could be corrected.

  41. Amit says:

    August 4th, 2011 at 6:06 am (#)

    Hi again Glenn

    I found the readme file and read that you allready concidered my suggestion from the previous post. Did you ever come to a concludion about that?

    Amit

  42. DMann says:

    August 30th, 2011 at 7:45 pm (#)

    Still working on site and have coming soon plugin installed at the moment.. however, my issue is that I recently made a different page( other than default wordpress home page) the static page and then made /blog the page for posts.. prior to that your plugin worked fine.. but when I made this change I now get the following error showing up in my slider content area.. ” Warning: Invalid argument supplied for foreach() in (my root folder info- I dont want to disclose)/twintigersmartialarts.com/wp-content/plugins/ft-password-protect-children-pages/ft-password-protect-children-pages.php on line 51….

  43. Phil says:

    October 28th, 2011 at 7:33 am (#)

    Having the problem with the word PROTECTED: coming up on all the other main navigation links when I click on a child page of a password protected parent page. I’m using the Headway theme and WordPress 3.2.1. How do I get round this as I thought this had been rectified on the 0.3 release.

  44. jason says:

    December 1st, 2011 at 5:48 pm (#)

    Thanks for this. Just wondering if there’s a default time period it will ask for the login again, or if the login will remain active until they close their browser?

  45. ed rosenthal says:

    December 3rd, 2011 at 12:30 pm (#)

    hi – thanks for the plugin, its working just fine. I’ve been trying to figure out how to change the password protect login though, and style it differently. On our site, we don’t enable the login for users, but i’ve tried to install a custom login plugin to see if that made any difference (it doesn’t). how does it really work? or how to style it differently? thanks in advance
    ed

  46. Nicki says:

    December 6th, 2011 at 2:49 pm (#)

    Just a quick thanks. The plugin is prefect!

  47. Debbie says:

    January 14th, 2012 at 4:36 pm (#)

    After I installed this plugin I got this warning message in all of my other widgets:

    Warning: Invalid argument supplied for foreach() in /hermes/web07/b1543/pow.cape/htdocs/wordpress/wp-content/plugins/ft-password-protect-children-pages/ft-password-protect-children-pages.php on line 72

    I am using WP 3.3.1, has this plugin been tested with this version? What can I do to fix this? This plugin would be perfect for what I need.

    Thanks,
    Debbie

  48. Russell Webster says:

    January 20th, 2012 at 11:15 am (#)

    Hi Glenn

    Seems to work except Child pages say: “Protected: Protected:” before title. Am using 0.3

    Would be great plugin if I could get rid of this

    Thanks

  49. Annedorte says:

    January 24th, 2012 at 5:04 pm (#)

    I have just installed the plugin, and it works fine in most browsers and it is just what I needed. However in IE 8, it doesn’t work and all pages are shown even though it clearly works in the others browsers and IE 9 – how do I solve that?

  50. Adam says:

    January 27th, 2012 at 3:23 pm (#)

    Hi Glen,

    I just noticed this on a site I’ve been working on.

    Your plugin seems to work perfect with the version of the site I’m running locally, but it doesn’t seem to work on the live server.

    Any idea what the issue might be?

  51. Daniel says:

    June 25th, 2012 at 11:16 am (#)

    Great plugin… works as described.

    Unfortunately works on versions before 3.4. Upgraded yesterday and plugin stopped working.

    Hopefully you guys can make this plugin compatible with future versions of WordPress.

    Thanks.

  52. Tracey says:

    July 12th, 2012 at 10:21 am (#)

    I’m currently using your plugin which is great. Could you tell me how I change the message “This post is password protected. To view it please enter your password below:”

    Many thanks

  53. Mark says:

    August 2nd, 2012 at 6:59 am (#)

    Hi

    The plugin works really well on the whole, but I have found a glitch in that it seems to conflict with the Responsive Slider plugin (http://devpress.com/plugins/responsive-slider/) as I have a slider embedded and it causes the following error message to appear in the slider text field of each slide:

    Warning: Invalid argument supplied foreach() in /wp-content/plugins/ft-password-protect-children-pages/ft-password-protect-children-pages.php on line 72

    I wondered if you might have any suggestions as to how to fix this?

    Thanks.

  54. Steve says:

    September 19th, 2012 at 4:25 pm (#)

    Hi Mark,

    I found this conflict as well but with the events calendar plugin. Solution is from wordpress.org forums:http://wordpress.org/support/topic/php-errors-custom-post-types-multiple-calls-to-the_content

    What you should actually do is go to the file ‘ft-password-protect-children-pages/ft-password-protect-children-pages.php’ and change all three instances of
    $ancestors = $post->ancestors;
    to
    $ancestors = get_post_ancestors($post);

    Best of luck.

  55. Jeff says:

    October 25th, 2012 at 10:28 pm (#)

    Just wondering if anyone knows the php to tell if your viewing a page from login “A” or login “B”

    Thanks.

  56. Niklas B says:

    January 23rd, 2013 at 4:35 am (#)

    I got a quick tweak, line 42, change it to:

    add_filter( ‘the_content’, ‘ft_password_protect_children_page_contents’, 20 );

    This makes sure it runs *after* all the other plugins that might alter the_content (otherwise that modules html is still shown, after the password form)

  57. Zach says:

    April 5th, 2013 at 5:23 pm (#)

    Your plugin is exactly what I needed. So thank you for that.
    Is there a way to change the message that is posted to someone who needs to enter the password?
    Currently: “This post is password protected. To view it please enter your password below:”

    I would like to say: “Please enter the provided dealer password”

    Thank You,