With this code snippet you’ll be able to show different menus to different users on your site, in other words, they are conditional menus.
The example we use in this post is having a default menu for all visitors, a different menu for logged in admins and yet another menu for logged in users who are not admins.
For the logged in users that are admins, the admin bar at the top of every site page is visible and for non-admins it is hidden.
There are many other conditions you can use to create a highly customized site. Some examples of what you can do are:
- Create a different menu for specific pages of your site (like the homepage or a 404 page)
- Unique menus for each of your categories or tag pages
- Different menus based on the data in a logged in user’s account information (for example, which courses they’ve bought)
Here’s a full list of conditional logic in WordPress (link takes you to the WordPress Codex)
The good news is this code works with navigation menus created in page builders or the customizer as long as they are loading menus that you’ve created under Appearance > Menus.
I have added comments to the code snippet below (all comments are preceded by //) to explain what each function does. There are 4 different functions in this snippet.
These need to be pasted into the bottom of your functions.php file to work. The best practice is to put them into a functions file in a child theme so that when the parent theme is updated your work is not overwritten. Here is a video show you how to create a child theme (this link takes you to YouTube).
You can also use each function described individually or you can mix and match or you can use all of them. Just make sure you replace the things I tell you replace otherwise it won’t work.
Function that creates the conditional menus
The first determines if someone is an admin or not and shows the appropriate menu. In this function you will need to replace the ALL CAPS text with the correct text referencing your menus.
If you create a menu for the admins and call it “Menu for Admins” then you will insert ‘menu-for-admins’ in place of ‘REPLACE-WITH-ADMIN-MENU-NAME’ in the beginning of the snippet below.
Create new menus for non-admin users that are logged and another for not logged in users.
Then replace ‘REPLACE-WITH-NON-ADMIN-MENU-NAME’ and ‘REPLACE-WITH-MAIN-MENU-NAME’ with the lowercase names for both of them. If you watch the video above that will make a lot more sense.
//This functions allows you to switch menus depending on if a user is logged into your site or not and, if they are logged in, what they're user role is function my_conditional_menus( $args = '' ) { [ if( is_user_logged_in() && current_user_can('administrator') ) { $args['menu'] = 'REPLACE-WITH-ADMIN-MENU-NAME'; //this menu will be shown ONLY to logged in admins } elseif ( is_user_logged_in() && !current_user_can('administrator') ) { $args['menu'] = 'REPLACE-WITH-NON-ADMIN-MENU-NAME'; //this menu will be shown to any logged in user that is NOT a site admin show_admin_bar(false); //removes the WP admin bar from the front end of the website } else { $args['menu'] = 'REPLACE-WITH-MAIN-MENU-NAME'; //this menu will be shown to all logged out users } return $args; } add_filter( 'wp_nav_menu_args', 'my_conditional_menus' );
Function that redirects users after logging in
It is convenient, and good for user experience, to redirect logged in users right after they log in. The next function in the snippet below redirects admins to the dashboard and non-admins to the services page.
If you want admins to be redirect somewhere else then replace wp-admin with with the page you want to redirect admins to.
And then replace my-account with the page you want to redirect non-admin users to.
//This function redirects to different pages based on user role function my_login_redirect( $redirect_to, $request, $user ) { //is there a user to check? if ( isset( $user->roles ) && is_array( $user->roles ) ) { //check for admins if ( in_array( 'administrator', $user->roles ) ) { return home_url('wp-admin'); //replace wp-admin with the page you want to redirect admins to return $redirect_to; } else { return home_url('my-account'); //replace services with the page you want to redirect non-admins to } } else { return $redirect_to; } } add_filter( 'login_redirect', 'my_login_redirect', 10, 3 );
Function that removes the annoying logout confirmation
Depending on how your theme is constructed, users may have to confirm the want to log out after they have clicked “log out”. This function fixes that, but it requires you to create the logout URL as /wp-login?action=logout in your menu system.
That is the old way of logging out, which some themes still use. This functions detects the old method and replaces it with the new method that doesn’t require logout confirmation.
//this function removes the logout confirmation, but it requires that the logout URL be created as /wp-login?action=logout in a custom menu item under Appearance > Menus function wpa_remove_menu_item( $items, $menu, $args ) { if ( is_admin() || ! is_user_logged_in() ) return $items; foreach ( $items as $key => $item ) { if ( 'Login / Register' == $item->title ) unset( $items[$key] ); if ( 'Logout' == $item->title ) { $items[$key]->url = wp_logout_url(); } } return $items; } add_filter( 'wp_get_nav_menu_items', 'wpa_remove_menu_item', 10, 3 );
Function redirects after logging out
Depending how your theme is coded, the most common WordPress behavior is that when someone logs out of the site they are redirected to the login page or they remain on the page that they logged out on.
The function below redirects them to a page of your choosing. In the example below it redirects to the homepage which is dynamically inserted where you see home_url().
//this function redirects the user to the home page after they log out function redirect_after_logout(){ wp_redirect( home_url() ); exit(); } add_action( 'wp_logout', 'redirect_after_logout');
Putting all together
Below you’ll find all the functions together in one easy to copy/paste block.
You can use each one described above individually or you can mix and match or you can use all of them. Just make sure you replace the things I tell you replace otherwise it won’t work.
//This functions allows you to switch menus depending on if a user is logged into your site or not and, if they are logged in, what they're user role is function my_conditional_menus( $args = '' ) { if( is_user_logged_in() && current_user_can('administrator') ) { $args['menu'] = 'REPLACE-WITH-ADMIN-MENU-NAME'; //this menu will be shown ONLY to logged in admins } elseif ( is_user_logged_in() && !current_user_can('administrator') ) { $args['menu'] = 'REPLACE-WITH-NON-ADMIN-MENU-NAME'; //this menu will be shown to any logged in user that is NOT a site admin show_admin_bar(false); //removes the WP admin bar from the front end of the website } else { $args['menu'] = 'REPLACE-WITH-MAIN-MENU-NAME'; //this menu will be shown to all logged out users } return $args; } add_filter( 'wp_nav_menu_args', 'my_conditional_menus' ); //This function redirects to different pages based on user role function my_login_redirect( $redirect_to, $request, $user ) { //is there a user to check? if ( isset( $user->roles ) && is_array( $user->roles ) ) { //check for admins if ( in_array( 'administrator', $user->roles ) ) { return home_url('wp-admin'); //replace wp-admin with the page you want to redirect admins to return $redirect_to; } else { return home_url('my-account'); //replace my-account with the page you want to redirect non-admins to } } else { return $redirect_to; } } add_filter( 'login_redirect', 'my_login_redirect', 10, 3 ); //this function removes the logout confirmation, but it requires that the logout URL be created as /wp-login?action=logout in a custom menu item under Appearance > Menus function wpa_remove_menu_item( $items, $menu, $args ) { if ( is_admin() || ! is_user_logged_in() ) return $items; foreach ( $items as $key => $item ) { if ( 'Login / Register' == $item->title ) unset( $items[$key] ); if ( 'Logout' == $item->title ) { $items[$key]->url = wp_logout_url(); } } return $items; } add_filter( 'wp_get_nav_menu_items', 'wpa_remove_menu_item', 10, 3 ); //this function redirects the user to the home page after they log out function redirect_after_logout(){ wp_redirect( home_url() ); exit(); } add_action( 'wp_logout', 'redirect_after_logout');
Responses