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?
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?
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.
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.
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.
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!
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.
Hi,
You can check out this detailed post on – detailed guide on web hosting and different types of web hosting servers. That would certainly help you in making the decision easier.
You can go for a good reputed managed hosting provider such as WpEngine.
Managed hosts do offer a lot of benefits and make life easy for beginners (since you do not have to stress regarding your hosting)
We are glad that you like the article. Thanks for your wonderful feedback!
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!
Can I just say what a relief to uncover someone that
really knows what they are talking about over the internet.
You actually understand how to bring a problem to light and make
it important. you most certainly have the gift of writing.
Thanks very nice blog!
Appreciate this post. Will try it out.
wordpress plugins make life so easy, isnt it
Thank you for publishing this awesome article.
I’m a long time reader but I’ve never been compelled to leave a comment.
I subscribed to your blog and shared this on my Facebook.
Thanks again for a great article!
I was stuck with this conditional logic problem for my wordpress site. I just downloaded the plugin you mentioned, and it worked. Thanks
Quality posts is the important to be a focus
for the users to go to see the web site,
This is exactly what this website is providing.