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.
Well an easy solution is CSS
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
With the conditional statement
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
Hope this helps you. keep us up to date.