Adding A Private Page Into The Navigation

WordPress Themes

If you use pages which have the state private, they will not be shown in the navigation. Nevertheless it can make sense to show this specific page in the navigation, if you want to provide easy access to this page for your users. The page should only appear to privileged users for sure. I will show you a little hack, which will let you exactly do this.

The Code


You have to copy this snippet to your theme at the place where the navigation is.
The function current_user_can checks, if the current user is privileged to read private pages. If not, then the page will not be shown for him.
Short notice: The 10 at get_permalink(10) is my ID for the page and has to be changed to your scenario.


by

Comments

25 responses to “Adding A Private Page Into The Navigation”

  1. Stefan Vervoort Avatar

    Thanks for this. Will use this in my upcoming theme!

  2. Alex Avatar

    Hey Stefan, glad to hear that some of our code will be included in your great website. 🙂

    Keep up the good work!

  3. […] Adding a Private Page into Navigation in WordPress – WordPress developers are finding new tricks day in day out. Here’s another one: add a […]

  4. […] to WpEngineer for this awesome recipe! Did this tip help you? If so, feel free to make a small […]

  5. Dawn Avatar

    Just implemented this in my design, worked perfectly, thanks for the hack!

  6. […] post, Michael has provided a quick and easy to implement WordPress Hack that will allow you to add pages to your WordPress navigation that are private (so only designated people such as administrators can see […]

  7. Alex Avatar

    Hey Dawn, glad it’s working for you!

  8. Dave Avatar

    Thanks for this, its exactly what I was looking for! Now to update my theme.

  9. […] Adding a Private Page into Navigation in WordPress – WordPress developers are finding new tricks day in day out. Here’s another one: add a […]

  10. Tim Nash Avatar

    I’m using Atahualpa Theme by BytesForAll on WordPress. My pages and posts menus are across the top. I think that is defined in header.php but I don’t know where abouts I should insert your code above to ensure that Private pages show up in the menu for users who are logged in. Can you help?

    Thanks!

  11. Michael Avatar

    Tim: I have no clue, how your theme is build. I think, the menu is in your header.php. look for a div id=”menu2″. Inside that div is a ul id=”rmenu”. After wp_listpages you can insert the code from

    if(current_user_can('read_private_pages')) :
    .....
    to endif ?>

    That should work.

  12. Josh Fialkoff Avatar

    Hi,
    I am trying to use this within the getcontent area of the loop, rather than on the sidebar, but I keep getting an error.
    Here is the code I have in my template. Can you please help me troubleshoot this?
    Thanks,
    Josh

    <a href="">For Authors only

  13. Michael Avatar

    sorry Josh, but the most of your code is missing.

  14. Josh Fialkoff Avatar

    Why yes it is! 😉
    Here’s the full code.
    -Josh

    <a href=””>For Authors only

  15. Josh Fialkoff Avatar

    The code does not seem to be coming through on my replies.

    Here is a link to the page code:

    http://dl.getdropbox.com/u/194649/code.rtf

    -Josh

  16. Michael Avatar

    Josh, the loop wasn’t closed correcly.

    
    
    	
    	

    And remember: the ID in get_permalink(10) must be replaced with the ID of your private page.

    Edit: fuck, the stupid editor adds a rel=”nofollow” to the line with the link. Replace the line with yours.

  17. Josh Fialkoff Avatar

    Thanks Michael!

    I used your code and replaced the post number with a test page I have set up (I plan to use the current page call in the final version).

    I am getting a syntax error now on the final line:

    Parse error: syntax error, unexpected $end in /data/14/1/99/60/1262060/user/1346477/htdocs/Wordpress/wp-content/themes/BCR/client_index.php on line 18

    I thought that this error was the result of having extra spaces after the final close tag, but I don’t have any spaces here, so I’m not sure what the problem is.

    Thanks again,
    Josh

  18. Josh Fialkoff Avatar

    BTW, the updated code is here: http://dl.getdropbox.com/u/194649/code.rtf

    Thanks,
    Josh

  19. Monky Avatar
    Monky

    Hi, how can I add a children page of the private page?

  20. Q Avatar

    I posted the code above in my header and and change the (10) value to my new page. while login i see the page but once i log out it is still there.

    http://256studio.com/members is the page ..

    Q

  21. Q Avatar

    ok i sorta fixed it i used pagemash to hid the link. once i log out it doesnt show… but once i log in.. it is there…
    Now i can post private events to my members.

    Q

  22. Maria Avatar
    Maria

    If i am not using a top menu but instead i am using a Widget (Pages) as my sidebar menu where do i have to insert the code.

    Thanks
    Maria

  23. Michael Avatar

    Maria, sorry but the code doesn’t work with the page widget.

  24. enad Avatar

    is it possible to hide private pages from the visitors and show it only to logged in users/subscribers. been looking for a plugin for ths. but only a plugin which is applicable for posts.

  25. Suzanne Bissonnette Avatar
    Suzanne Bissonnette

    Yes… I use this plugin:

    • Members Only Menu Plugin

    http://wordpress.org/extend/plugins/members-only-menu-plugin/

    Works fine, just one click to hide the page if not a connected user.

    Have fun !