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 –

What is conditional logic?

conditional-logic-if-else-statements

Our everyday life involves making up decisions based on different conditions. It is generally on the basis of statements such as – If this happens, then that happens.

For example, if my car's petrol tank has appropriate fuel, I can drive to my destination.

The same logic is also used in programming languages and coding.

Why Conditional logic is required to be added in a WordPress Menu?

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]

How to add Conditional logic in WordPress Menu?

if-then-else-statement-conditional-logic

Above image is an example of conditional logic used in programming. In WordPress, we do not have to do the coding manually, we can achieve this with the help of a plugin.

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

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

2: After installation, activate the if menu plugin.

3: Then click on Appearance and select Menus option.

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

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

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 the 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 Right to Left

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.

It is extremely important to take a backup of your site before making any changes in the code. In fact, it is one of the most common mistakes people make with their wordpress websites. Check out this article on what are the common mistakes people make with their wordpress websites and how to avoid them.

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;
}

It is important to 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.

Did this article help you, Would you please post your opinion below.

5 Comments

  1. Greetings! Very helpful advice within this article!
    Thanks for sharing! I have you book marked in my google bookmarks.
    Numerous other people shall be benefited from your writing.
    Cheers!

  2. Hi there would you mind letting me know which web host you’re utilizing?
    I’ve loaded your blog in 3 completely different web browsers and I
    must say this blog loads a lot faster then most.

    Can you suggest a good hosting provider at a reasonable
    price? Cheers, I appreciate it! I am not sure where you’re getting your
    info, but great topic. I was looking for this.

  3. Wow, wonderful blog layout! How long have you been blogging for?
    you make blogging look easy. The overall look of your site is great, as well
    as the content!

Leave a Reply

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

Post comment