Share this post:
Share on facebook
Share on twitter
Share on reddit
Share on linkedin
Share on pinterest
Share on digg
Share on stumbleupon
Share on pocket
Share on email
Share on print

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.

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.

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.

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().

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.

Share on facebook
Share on twitter
Share on linkedin
Share on pinterest
Share on email

Leave a Comment

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

Do NOT follow this link or you will be banned from the site!