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

Conditional Logic PHP Snippets

If you are trying to set up conditional logic rules with a PHP snippet and the conditional logic is not working as intended it’s likely caused by the Auto-Insert location you selected.

The default “Run Everywhere” location for PHP snippets is set up to run very early (using the plugins_loaded hook) for more flexibility on what features you can extend using the WPCode PHP Snippets. Due to these snippets being executed very early, most of the data required for the Conditional Logic rules to work is not yet available so most rules won’t have any effect.

In version 2.0.8 of WPCode we added a new Auto-Insert location for PHP snippets called “Frontend Conditional Logic” which is automatically selected when you enable Conditional Logic (you can always switch back to Run Everywhere).

This new location executes snippets on a hook that runs later (the wp hook), after most of the page data is loaded allowing you to take advantage of all the Conditional Logic rules available in WPCode.

It’s important to note that while this later hook is great for conditional logic it might not be the best one for certain filters or actions that run before it and in that case if you want to limit where your snippet is executed you will have to include those checks in the PHP code.

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.