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

Run Code Snippets on Demand

Would you like to run a piece of PHP code once, at the exact moment you choose, instead of on every page load? All you need is WPCode and a PHP snippet.

In this guide, we’ll show you how to run PHP snippets on demand with WPCode.

On Demand is a location for one-off tasks, such as clearing a cached value or updating a setting. A snippet that uses it never runs on its own. It runs once each time you click a button in the snippet editor.

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

Creating a PHP Snippet

To get started, we’ll create a new PHP snippet to hold the code you want to run.

From your WordPress admin sidebar, go to Code Snippets » + Add Snippet.

The WPCode Add Snippet screen with the + Add Snippet item highlighted in the Code Snippets admin menu

On the Snippet Library tab, hover over the Add Your Custom Code (New Snippet) card. Then click + Add Custom Snippet.

The Add Your Custom Code (New Snippet) card with an arrow pointing at the + Add Custom Snippet button

WPCode asks what type of code you’re adding. Choose PHP Snippet, since the On Demand location is only available for PHP snippets.

The WPCode code type picker showing eight snippet types with an arrow pointing at PHP Snippet

In the snippet editor, type a name for your snippet in the title field at the top. Then add your code to the Code Preview box.

The snippet title field outlined in red and filled in with Clear Featured Products Cache, above a Code Preview box containing a delete_transient line

In our example, the code deletes a cached list of featured products with the delete_transient() function. The list then rebuilds on the next page load.

Note: You don’t need to type an opening PHP tag. The editor already shows one above your first line of code.

With the title and code in place, the next step is to choose the On Demand location.

Choosing the On Demand Location

Next, scroll down to the Insertion box. A new PHP snippet starts on the Run Everywhere location. That location runs its code on every page load.

Next, click the Location field to open the list of locations.

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

Under PHP Snippets Only, choose On Demand.

The location picker open on the Global category showing the PHP Snippets Only group with an arrow pointing at On Demand

Leave the Insert Method on Auto Insert. On Demand is one of the Auto Insert locations.

To learn how the other PHP locations decide when your code runs, see our guide on running PHP snippets with conditional logic.

Running Your Snippet

Once On Demand is selected, scroll back to the top of the editor. The Active and Inactive status toggle is gone. An Execute Snippet Now button sits in its place.

When your code is ready, click Execute Snippet Now.

An arrow pointing at the Execute Snippet Now button at the top of the WPCode snippet editor, next to Save Snippet

WPCode saves your snippet and runs its code once, right away. The code runs as soon as you click. It’s a good idea to read it over first.

The page reloads with the message Snippet updated & executed.

The Edit Snippet screen in WPCode showing the confirmation message Snippet updated and executed

When WPCode runs a snippet this way, it saves the snippet as inactive. The code doesn’t run again on the next page load. It runs again only when you click Execute Snippet Now.

The message confirms that the code ran. To check the result, look at whatever your code changed, such as a setting or a cached value.

Saving a Snippet to Run Later

You don’t have to run a snippet the moment you create it. If you’d like to save it and come back later, click Save Snippet instead. On a snippet you’ve already saved, this button reads Update.

An arrow pointing at the Save Snippet button at the top of the WPCode snippet editor, next to Execute Snippet Now

When you’re ready to run it, go to Code Snippets » Code Snippets and click the snippet’s title. In the snippets list, the Location column for this snippet reads On Demand.

The WPCode Code Snippets list showing a snippet titled Clear Featured Products Cache with the location On Demand and an arrow pointing at its title

At the top of the snippet editor, click Execute Snippet Now.

An arrow pointing at the Execute Snippet Now button at the top of the Edit Snippet screen for a saved snippet

You can run the same snippet as many times as you need. Each click saves any changes you’ve made to the code, then runs it once.

That’s it! You now know how to run PHP snippets on demand with WPCode. 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 on demand.

Will my On Demand snippet ever run on its own?

No. WPCode doesn’t run On Demand code when someone loads a page, either on the front end of your site or in the admin area. The code runs only when you click Execute Snippet Now.

How do I check what my code did?

To see what your code did, look at whatever it changed, such as a setting, a cached value, or a page on your site. The Snippet updated & executed. message confirms the run itself. WPCode runs the code on the server while it saves the snippet. Any text your code prints, such as from an echo statement, stays out of view.

Does Smart Conditional Logic apply to my On Demand snippet?

Yes. If you turn on Enable Logic for an On Demand snippet, WPCode checks your Smart Conditional Logic rules when you click Execute Snippet Now. The code runs only if the rules match at that moment. That moment is always in your WordPress admin area. The Snippet updated & executed. message appears either way. For a one-off task, leave Enable Logic off so the code runs every time you click.

Who on my site can run an On Demand snippet?

Administrators can. You can also let other user roles run PHP snippets by giving them the Edit PHP Snippets permission. Our guide to WPCode access control shows you how.

Can I run an HTML, CSS, or JavaScript snippet on demand?

No. On Demand appears under PHP Snippets Only, and WPCode only lets you choose it for a PHP snippet. On other snippet types, the location shows in the list but can’t be selected.

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.