Documentation, Reference Materials, and Tutorials for WPCode
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.
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.

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

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

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.

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.
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.

Under PHP Snippets Only, choose 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.
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.

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.

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.
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.

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.

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

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.
Below, we’ve answered some of the most common questions about running PHP snippets on demand.
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.
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.
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.
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.
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.
Future-proof your website with WPCode Snippets and improve the way you manage code across all your websites.