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

Would you like a PHP snippet to run only on certain pages of your site, such as your single blog posts? All you need is WPCode and a PHP snippet.

In this guide, we’ll show you how to run PHP snippets with Smart Conditional Logic using the Frontend Conditional Logic location.

Before you get started, make sure WPCode is installed and activated on your WordPress site.

How Locations Decide When PHP Snippets Run

An HTML or JavaScript snippet prints its code at a spot on the page. A PHP snippet works differently.

It runs its code at a set moment while WordPress loads. The snippet’s location decides that moment.

Run Everywhere is the location every new PHP snippet starts on. It runs your code very early, as soon as WordPress has loaded its plugins. Developers know this as the plugins_loaded hook.

Running this early gives your code the most flexibility. It can extend things WordPress sets up later, such as custom post types.

At that moment, though, most of the page data that conditional logic rules check hasn’t loaded yet. WordPress hasn’t worked out which page the visitor asked for. Rules that look at the page, such as Type of page, Post type, or Page URL, have nothing to check yet.

That’s why WPCode has a location built for PHP snippets with conditional logic, called Frontend Conditional Logic. It runs your code later, once WordPress knows which page is loading and has fetched that page’s content, just before the page is displayed. That’s the wp hook.

At this point, every conditional logic rule has the information it needs.

Turning On Conditional Logic for a PHP Snippet

To get started, we’ll open a PHP snippet and turn on its conditional logic.

From your WordPress admin sidebar, go to Code Snippets » Code Snippets. Then click the title of the PHP snippet you want to limit.

The Code Snippets list in WPCode with an arrow pointing at a PHP snippet titled Post Sign-Off Message

In our example, the snippet adds a short thank-you message to the end of post content. In the Insertion box, you’ll see that the snippet’s Location is Run Everywhere. That’s where every new PHP snippet starts.

The Insertion box of a PHP snippet with the Location set to Run Everywhere

Next, scroll down to the Smart Conditional Logic box and turn on the Enable Logic toggle.

The Smart Conditional Logic box with Enable Logic switched on, an arrow pointing at the toggle, and a notice saying the location was switched to Frontend Conditional Logic

Once you switch the toggle on, WPCode changes the snippet’s location to Frontend Conditional Logic for you. A notice above the toggle confirms the change.

If you scroll back up to the Insertion box, the Location now reads Frontend Conditional Logic.

The Insertion box of a PHP snippet with the Location now set to Frontend Conditional Logic

WPCode makes this switch on a saved PHP snippet that uses Run Everywhere. On a new snippet you haven’t saved yet, or one that uses a different location, you can choose Frontend Conditional Logic yourself.

Choosing Frontend Conditional Logic Yourself

Picking the location by hand works for any PHP snippet, whichever location it uses now.

In the Insertion box, click the Location field to open the list of locations. In our example, the snippet is still on Run Everywhere.

The Insertion box of a PHP snippet with the Location set to Run Everywhere and an arrow pointing at the Location field

The list opens on the Global category, with the PHP Snippets Only group at the top. Under PHP Snippets Only, choose Frontend Conditional Logic.

The location picker open on the Global category showing the PHP Snippets Only group with Run Everywhere marked Selected and an arrow pointing at Frontend Conditional Logic

The same list lets you switch back to Run Everywhere at any time.

Adding Your Rules and Saving

With the location set, add your rules in the Smart Conditional Logic box, exactly as you would for any other snippet. In our example, a Type of page rule set to Single post/page runs the sign-off message on single posts only. Our guide on using Smart Conditional Logic walks through building rules and groups.

When your rules are ready, click Update at the top of the snippet editor. On a snippet you haven’t saved yet, this button reads Save Snippet instead.

An arrow pointing at the Update button at the top of the WPCode snippet editor, next to the Inactive status toggle

Your rules take effect once the snippet is saved and its status toggle, just left of the Update button, reads Active.

To check the result, open a page your rules match and look for the snippet’s effect. Then open a page they don’t match, where the snippet doesn’t run.

Choosing Between Frontend Conditional Logic and Run Everywhere

Frontend Conditional Logic is the best fit when your code changes what visitors see on a page. That includes filtering post content, adding scripts or styles, adding code to the header or footer, and redirecting visitors away from a page.

WordPress runs all of these after the wp hook. Your snippet’s code is in place before any of them happen.

Because Frontend Conditional Logic runs later, it isn’t the right fit for code that hooks into something WordPress runs before the wp hook. Run Everywhere is the best fit when your code needs to run earlier than that, or outside the front end. For example, it’s the right choice for registering custom post types and taxonomies, or for hooking into init or after_setup_theme. It’s also the right choice for code that runs in the admin area, during AJAX requests, or for the REST API.

You can still limit a Run Everywhere snippet to certain pages. Add the page check inside your PHP code, in the part that runs later. For example, in a function that filters post content, use the is_single() function to return early on any other page.

That’s it! You now know how to run PHP snippets with conditional logic using the Frontend Conditional Logic location. Next, take a look at our complete guide to snippet locations to explore the other places your snippets can run.

Frequently Asked Questions

Below, we’ve answered some of the most common questions about running PHP snippets with conditional logic.

Will my Frontend Conditional Logic snippet run in the WordPress admin?

No. Frontend Conditional Logic runs only when a visitor loads a page on the front end of your site. It doesn’t run in the admin area, during AJAX requests, or for REST API requests. For code that needs to run in those places, use Run Everywhere.

Why don’t my page rules match on a PHP snippet?

The most likely reason is the snippet’s location. On Run Everywhere, a PHP snippet runs before WordPress knows which page is loading, so rules like Type of page have nothing to check. Switch the snippet’s location to Frontend Conditional Logic and click Update, and those rules can match.

Can I use conditional logic with Run Everywhere?

Yes. Rules that don’t depend on the page, such as Logged-in or Day of the Week, work with Run Everywhere. Rules that check the page, such as Type of page, need Frontend Conditional Logic to match.

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.