The title pretty much explains itself, I'm looking for a way/plugin to hide the menu link to a specific page from users that aren't logged in.
2 Answers
No plugin or PHP code is needed to do this. If you look at your HTML body class, you will see that WP helpfully adds the class of "logged-in" for any users who are registered and logged into the site.
With this info along with the class name you give for the particular menu link you want to hide/show, you can draft a CSS rule that will hide that.
In your menu settings, first turn on CLASS field by going to the top right of your screen and clicking on "screen options" and check the box for CLASS fields. This will now show a class field for each menu item in your menu.
So for the menu you want to show/hide, add a class name such as "hide".
Then in your theme's CSS, you can add the following:
.menu li.hide { display:none; }
.logged-in .menu li.hide { display:block; }
So the above CSS says to hide the menu link with the class name of "hide" and if body class has "logged-in", then show it.
Of course you may need to change the CSS classname to suit your theme's CSS class name. For example yours may not be ".menu".
<?php
if (is_user_logged_in())
// echo the link
else
// Dont echo the link
?>
Doesn't require a plugin.
-
-
@s_ha_dum Are you sure? codex.wordpress.org/Function_Reference/is_user_logged_in– TwiftySep 1, 2013 at 19:01
-
is_user_logged_in
is a perfectly good function but if this is a menu created bywp_nav_menu
, or related menu functions, how does this code help?– s_ha_dumSep 1, 2013 at 19:08 -
@s_ha_dum Ahh, nice catch, I took his 'menu' in the sense of sidebar/navigation menus.– TwiftySep 1, 2013 at 19:17