Hide menu item in Wordpres Nav if logged in

Glenn Glenn's Blog 0 Comments

Something we’ve come across lots of times in WordPress is how to hide a specific menu item in WordPress menus if the user is logged-in, or indeed logged out. A common application for this would be if you have a registration page which you obviously don’t want to continue to show if the user has already registered (they are logged in).

Previously we have delved into the site head and the menu itself to use WordPress Conditional Tags/Statements. So for example:-



<?php
if ( is_user_logged_in() ) {
echo 'Welcome, registered user!';
} else {
echo 'Welcome, visitor!';
}
?>

So in respect of the Nav you could use the following in in header.php (in place of the regular wp_nav_menu), under div access/role


<?php
if(is_user_logged_in()){
wp_nav_menu( array( 'container_class' => 'menu-header', 'theme_location' => 'primary', 'exclude' => '39' ) );
}
else{
wp_nav_menu( array( 'container_class' => 'menu-header', 'theme_location' => 'primary' ) );
}
?>

Just add the page id to exclude to the logged in user.

An easier Way!

The above is fine and dandy if you can access your site files and/or are happy to modify the template(s) code, but there is an even easier way and that is via the following plugin:-

Simple Access Control

The plugin created by Peter Wooster is freely available on the WordPress plugin repository via the link above and this makes removing pages from the WordPress Nav on logged in status an absolute breeze. Indeed it also hides the page content itself as to whether the user is logged in or logged out:-

Hide menu item in Wordpres Nav if logged in Hide menu item in Wordpres Nav if logged in simple access control plugin

 

A very useful plugin!

About the Author
Hide menu item in Wordpres Nav if logged in Hide menu item in Wordpres Nav if logged in 0da77002febbfe28d15144a57bbcc2c9 s 180 d mm r g

Glenn

A highly experienced WordPress Web Developer, Front-end and Back-end Developer & New Media Specialist with extensive knowledge of a wide spectrum of technologies in the Development and Creative Industries built up over a number of years.

Leave a Reply

Your email address will not be published. Required fields are marked *