Setup Menus in Admin Panel

  • LOGIN
  • No products in the cart.

By Team Bitgog , Last updated on March 15, 2019 , This post may contain affiliate links. Read my disclosure policy.

How to Add Conditional Logic to Menus in WordPress

In this article, we will understand how to add conditional logic to menus in WordPress.

Before proceeding with how to add conditional logic, let us first understand why is Conditional logic required. There might be certain scenarios where you want to hide certain menu items for specific user roles. Alternatively, you might want to show some extra menu options for admin roles or for logged in users such as Logout option is only visible if the user has logged in or we can say that the Login option is only visible when the user is logged out of the website. So, using conditional logic, you can show or hide menu items based on the type of user role, post or where on the site the user is.

[/vc_column_text]

Let us add Conditional Logic to menus using If Menu plugin. In order to do this, we need to follow below mentioned steps:-

Step 1: Click on the plugin option then select add new option and install if menu plugin.

Step 2: After installation, activate the if menu plugin.

Step 3: Then click on Appearance and select Menus option.

Here, you will see the existing website menus in this section.

Step 4: Select the menu item where you want to add the conditional logic.

Step 5: After choosing the menu item, you will see an option “change menu item visibility”. Select this option since this will help you to change the visibility of the menu item.
Now, you can select Show/Hide according to your requirements and you can choose the condition associated with it in the adjacent drop-down as shown in the screenshot below. For example, you can show the menu item only if user is Administrator or Editor and you can hide it for other user roles.

So, you can apply conditional rules on basis of:

1. User State: whether the user is logged in or not

2. User Roles: whether the user is Administrator, Editor, Author, Contributor or Subscriber

3. Page Types: Front Page, Single Post, Page

4. Device: show/hide menu item if the user is visiting the site from a mobile

5. Language: if language is RTL

Add up your own conditions

You can also use AND and OR conditions. You can use AND option when you want a specific menu item to be visible to the user only when both the conditions are true. And you can use OR option when you want that the menu item should be visible to the user when at least one condition is true.

For example, if you only want to show a menu item to users with Administrator or Editor role then, you can use OR option and select option ‘User is Administrator’ in first drop-down and in second drop-down, select ‘User is Editor’.

So, after you have configured the conditional rules, click on the Save button to save your changes.

Add a custom visibility rule

If you want to apply a conditional rule which this plugin does not offer, then in that case, you can add a custom rule. WordPress provides a lot of conditional tags that you can use.

Add the below code snippet to function.php file of your theme. Do make sure that you keep a backup of functions.php file before making any changes so that you can always restore it back if anything goes wrong.

add_filter( ‘if_menu_conditions’, ‘custom_new_menu_conditions’ );

function custom_new_menu_conditions( $conditions ) {
$conditions[] = array(
‘name’ => ‘[name of the condition]’, // mention here name of the condition
‘condition’ => function($item) { // callback – must return TRUE or FALSE
// perform any processing if required
return [TRUE or FALSE]; // This will return TRUE or FALSE depending if the condition is met
}
);
return $conditions;
}

Do test thoroughly that your conditional rules are working correctly.And, Voila let the magic begin!!

So, this is how we add conditional logic to menus in WordPress.

If you think that the above article helped you, would you please share it with your friends on social media because it might help your friends or family. Would you please also post your opinion or queries or feedback below.

0 responses on "How to Add Conditional Logic to Menus in WordPress"

We would love to know your opinion, please share it below:

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

Copyright © 2019 Bitgog.com All Rights Reserved