Add Menu For Logged In Users

On a recent project I had to incorporate a different set of menus for users who were logged in.
I used the function is_user_logged_in() to see if the user was logged in, with an if/else statement to change out the menu.


function rjs_wp_nav_switch( $args = '' ) {

if( is_user_logged_in() ) { 
	$args['menu'] = 'logged-in';
} else { 
	$args['menu'] = 'logged-out';
} 
	return $args;
}
add_filter( 'wp_nav_menu_args', 'rjs_wp_nav_switch' );

Change the 'logged-in' to the name of the menu to show when the user is logged in.
Change the 'logged-out' to the name of the menu to show when the user is not logged in.
Set your menu as the logged-out menu. When a user is logged in the if/else statement will switch out the menus for you.

Leave a Reply

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