WordPress.org

Ready to get started?Download WordPress

Forums

Hide Page from logged out users (8 posts)

  1. gregw89
    Member
    Posted 3 years ago #

    I have a page that I want to be visible on my menu by only logged in users. How could I do this?

  2. oturia
    Member
    Posted 3 years ago #

    Are you using a WordPress list function to list out your pages or are you listing them manually?

    The easiest way to do it would be to open the page in the page editor, look to the right hand side under "Publish", and change the "visibility" from public to private.

  3. gregw89
    Member
    Posted 3 years ago #

    I do not believe I am listing them manually. They are listed in the header.php as wp_list_pages. If I change it to private, will people who log into my site be able to see it?

  4. oturia
    Member
    Posted 3 years ago #

    Yes. People who are logged into WordPress will be able to see it on the front end.

  5. gregw89
    Member
    Posted 3 years ago #

    I set it to private, but that removes it from the menu for everybody, including me. Is there something else I need to do?

  6. oturia
    Member
    Posted 3 years ago #

    How often do you add/remove pages from the menu? One thing you could do is list them out manually, then wrap the private page in a "is logged in" conditional statement so that only logged in users can view them.

    It would look something like this:

    <?php if (is_user_logged_in()){ echo "<li>link to page url</li>" }; ?>

    Or, you could not alter the lists at all, and just add that to the end of the wp_list_pages tag, before the closing tag. That would list the page there, but just at the very end of the menu.

    Does that make sense?

  7. madivad
    Member
    Posted 2 years ago #

    Sorry this is late... but it'll help others I suppose...

    Depending on the theme you're using and if it applies the "logged-in" class to the body (I believe the default theme does, as does the theme I use, as does the theme used by the author of the hint) you can use the method described here. Works as described and is very easy.

    - Create a child theme to apply a custom style as detailed below, or use a custom style sheet plugin (I use the the Custom Style Plugin, and it does this flawlessly -- plugin hasn't been updated for a while, and the author says it works for wordpress up to 2.9.2, but I am still using it on 3.2.1 with no issues -- I've been using this plugin for years!)

    - add the following code to that CSS:

    #nav li.logged-in-nav {
    display: none;
    } 
    
    body.logged-in #nav li.logged-in-nav {
    display: list-item;
    }

    - Then on the menu item you create for that page, simply add a css rule for it. ie, there is a box for CSS Classes, add logged-in-nav to that textbox

    For this to work, you'll need to actually have a primary list defined then edit the actual menu item to find that CSS Classes field.

    Downside, the link is still available in the source code for the page, but unless someone goes looking they wont find it.

    Now, if only someone could tell me how to do exactly the same for users that ARE logged in -- ie, I want to hide some pages from logged in users, but there's no "not-logged-in" or "logged-out" class for the body tag.

    If someone could tell me how to create such a class to that's added when the page is generated, I'd be stoked!!!

  8. madivad
    Member
    Posted 2 years ago #

    I forgot to mention in the above code, you must change the #nav to the name of the ID of the unordered list that contains the list/menu items of the relevant menu.

    I have since created a full "how-to-do" on my blog since I was after not only this feature, but another where I only showed menu items to guests and not members--basically the opposite.

    To only show items to logged in users here.

    To show a particular menu item to guests only (something I have been searching for for a while), you can view a more recent post here.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags