3

I've started writing a plugin that allows the administrator to select specific pages and set them to be visible only to logged-in users; the plugin optionally applies the restriction to all child pages too. When a site visitor attempts to view any of these pages, they get redirected to the login page.

So far, so good. It all works to that point, and duplicates functionality I've seen in existing plugins.

However, what I also want to do is to automatically remove all of those pages (including child pages, where specified) from the front-end site's menus. I don't want them visible at all unless you're logged in.

I've seen solutions where people create two menus and switch between them depending on logged-in status, but that's an extra step I'd rather not have.

I thought I could perhaps approach this by adding a filter or action hook for 'wp_nav_menu_items' in the plugin code, but I've had no success with that yet.

Any thoughts?

Thanks Phil


Additional details (smaller chunk of the problem):

Here's some code from my plugin that I'm trying to use to hook into the menu items:

add_filter('wp_nav_menu_items','ppa_filter_visible_menus');

function ppa_filter_visible_menus($nav) {
    die("filter menus");
}

The problem I have is that ppa_filter_visible_menus is not called.

3
  • 2
    Hi, Phil, welcome to WPSE. Please, show the code you are trying to use.
    – brasofilo
    Commented Jan 18, 2013 at 16:45
  • Hi. I have no code for what I'm trying to do. I have code for the part of my plugin that works (so I won't bother posting that). What I need are some ideas for how to selectively hide/remove all menu items that correspond to a given list of page IDs.
    – Phil Piper
    Commented Jan 18, 2013 at 20:09
  • 1
    Well, the filter is inside the function with the same name, see if you can get some inspiration from there.
    – brasofilo
    Commented Jan 18, 2013 at 20:16

1 Answer 1

0

Well an easy solution is CSS

<!--    HIDES MENU ITEM IF THE USER IS NOT LOGGED IN   -->
<?php if (is_user_logged_in()){
echo "";}
else {
echo "<style type='text/css'>
.menu-item-58 {display:none;}
</style>";
};
?>

But if you are trying to code it down see the source of this plugin http://wordpress.org/extend/plugins/if-menu/ -- it will help you alot.

While if you just want a coding answer - a simple one - replace the header

<ul>
  <?php wp_list_pages(); ?>
</ul>

With the conditional statement

if user not logged in
show
<ul>
  <?php wp_list_pages('exclude=17,38' ); ?>
</ul>

where exlude are the pages id you don't want a not logged in user to see.

while using else commend to show all menu to logged in user

else 
<ul>
  <?php wp_list_pages(); ?>
</ul>

Hope this helps you. keep us up to date.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.