How a snippet runs
The How it runs setting decides what makes a snippet run. It's in the settings panel, and it applies to PHP and HTML snippets. CSS and JavaScript always run automatically.
| Setting | What happens | Good for |
|---|---|---|
| Automatically | The snippet runs on its location (Where) on every request where its conditions match | Almost everything |
| As a shortcode | The snippet runs only where you place its shortcode, in a post, page, widget or page builder | Content you place by hand: banners, forms, calculated values |
| Only when I click Run | The snippet never runs on its own. It runs once when you click Run in the editor (or with WP-CLI) | One-off tasks: clean-ups, data fixes, bulk updates |
Automatic and shortcode snippets only run while they're switched on (Active). The Run button works whether the snippet is on or off.
Automatically
The default. The snippet runs at its Where location, in order of Priority (lower runs first, default 10). See Where snippets run.
As a shortcode
Choose As a shortcode, save, and place the shortcode in your content:
[snipfire id="12"]
Replace 12 with the snippet's ID (shown in the sidebar and the editor). You can also give the snippet its own tag in the Shortcode field, for example promo_banner, and use [promo_banner]. Attributes are passed to your code. See Shortcodes.
Conditions still apply: if they don't match, the shortcode prints nothing.
Only when I click Run
For code that should run once, on demand. WordPress is fully loaded when it runs.
// Delete all transients whose names start with "myplugin_".
global $wpdb;
$count = $wpdb->query( "DELETE FROM {$wpdb->options} WHERE option_name LIKE '\_transient\_myplugin\_%'" );
echo "Deleted $count rows.";
Save it, then click Run. It doesn't need to be switched on, and leaving it off means nothing else can trigger it.
The Run button
Run is in the toolbar for PHP and HTML snippets. It saves the snippet, then runs it once now, ignoring conditions, and shows what happened under the editor:
- Ran without errors. plus anything the snippet printed or returned, under Run output.
- Ran without errors (no output).
- Failed: with the message and line, if it failed. An error during Run is reported to you; it doesn't switch the snippet off.
A snippet with a syntax error isn't run: you'll see the error instead.
You can use Run on any PHP or HTML snippet, not only ones set to Only when I click Run, which makes it handy for checking what a snippet prints.
From the command line: wp snipfire run <id> (see WP-CLI).
Be careful with Run on code that changes data: it really runs on your site.
Something unclear or missing? Tell us