WPCode Logo

WPCode Documentation

Documentation, Reference Materials, and Tutorials for WPCode

Most Popular

Get free tips and resources right in your inbox, along with 500+ others

Follow Us

How To Use Snippets Smart Conditional Logic

Would you like to limit where WPCode snippets are automatically inserted on your website? The Smart Conditional Logic options allow you to create specific rules for each snippet changing the way they get executed. Let’s get started.

Basic settings

The first step in creating a set of rules is to enable the conditional logic using the toggle at the top:

The second option you have is whether the rules you create below will either Show or Hide the snippet. Sometimes you want to limit the snippet from being executed for very specific set of rules and it might be easier to define where it should not execute instead of having to define all the other scenarios where it should, that’s why, using this field, you can turn things around.

Types of rules

The rules you can create using the Conditional Logic fields are split up in the following main categories:

  • User
  • Page

User

The User category, as the name suggests, allows you to add restrictions based on your website visitor and includes the following options:

  • Logged-in: choose whether the rules should apply only for logged-in or logged-out users.
  • User Role: choose the User Role to limit the snippet execution by.

Page

The Page rules are used in relation to the page that the visitor is seeing and where the snippet is executed.

  • Type of page: this gives you a quick way to choose from the basic WordPress types of pages: Homepage, Archive, Single Post/Page, Search Page, 404 Page, Author page
  • Post type: using the post type option you can make a group of rules more specific, let’s say you use the type of page Single Post/Page but you want to limit that to the post type “cars”, you would use this option to specify the post type. The option gets automatically loaded with the available post types on your site
  • Referrer: next up is the Referrer, this allows you to execute specific snippets for certain referrers – this is a text input selector so you can choose “contains” from the comparison field for an easier way to match. Please note that the referrer option might not always work as intended when using cache so it’s more useful for pages which never get cached, like the checkout or cart page of your site.
  • Taxonomy page: This option allows you to choose a taxonomy that is relevant for the page being viewed from all the available taxonomies, for example, if you chose Archive for the type of page you can combine it with this one to make the rule specific to a certain taxonomy.
  • Taxonomy term: Allows you to choose taxonomy terms to apply the rule to. As you type, you will get suggestions from all the terms available on your site and you can choose multiple terms for your rule.
  • Page URL: This is a very powerful one as it allows you to target specific URLs or using the “Contains/Doesn’t Contain” operator you can target more pages on your site.

Rule Groups

You may be asking: What if I want to use multiple rules from the same option?

As you may have noticed, in a group of rules the relation between the rules is “AND” which means all the rules in a group have to be true in order to have the conditional logic apply to the snippet. You can make things more flexible by adding multiple groups:

This allows you to use the same rule multiple times. For example, if you want your snippet to be shown only to logged-in users that have the role of Administrator and the role of Editor your rules will look like this:

That’s it! Now you know how to use Smart Conditional Logic to create powerful rules for your snippets.

Still stuck? How can we help?

Last Updated on

Get Started Today & Add Your Own Snippets

Future-proof your website with WPCode Snippets and improve the way you manage code across all your websites.