Skip to content

Early-bird lifetime licence: $199 once, for the first 200 buyers only. See the offer

All docs

Export snippets as a plugin

Snipfire can turn snippets into an ordinary WordPress plugin: a zip you install on any site, which runs those snippets without Snipfire. Use it for a client site that shouldn't have a code snippets plugin, to hand over finished work, or simply because you never want to be locked in.

The plugin has no crash protection. Snipfire catches errors in snippets and switches a broken one off; the exported plugin doesn't. Test it on a staging site first.

Export

Pick what goes in:

  • Some snippets: tick them in the sidebar and click Plugin in the bulk actions bar.
  • A folder: click the ⇩ button on the folder's row (Export as a plugin). Its subfolders come along.
  • One snippet: open it, then ⋯ → Export as a plugin….

The Export as a plugin screen asks for:

Field What it is
Plugin name The name on the Plugins screen. Default: the snippet's title (for one snippet), the folder's name, or My snippets
Folder name (slug) The plugin's folder and file name: lowercase letters, digits and dashes. It can't start with snipfire (that name belongs to Snipfire and its add-ons)
Version Default 1.0.0. Raise it each time you export again

Below, a report lists each snippet with In the plugin (Exported, Left out or Can't be exported) and Notes. Download the plugin (… snippets) downloads the zip. Install it on the site where it should run, under Plugins → Add New Plugin → Upload Plugin.

Exporting needs full Snipfire access (administrators). It works while SNIPFIRE_DISABLE_PHP is set, since nothing runs on this site; with DISALLOW_UNFILTERED_HTML, Snipfire is view only and exporting is refused. See Locking Snipfire down.

What the plugin reproduces

  • PHP on any hook and priority, or straight away (Run immediately).
  • Shortcodes, with their attributes, including your own tags.
  • HTML in every location: the head, after <body> opens, the footer, before or after the post content, before or after paragraph N, with the excerpts, and before, after or between posts in lists.
  • CSS and JavaScript as files, in the same places: the front end, the admin area, the login page and the block editor, with defer or async as set. SCSS and LESS go in as their compiled CSS.
  • Settings form values as they are when you export: $settings in PHP and {setting:key} are filled in.
  • The everyday conditions: request type, logged in, user role, URL, query parameter, cookie, device, page type (WooCommerce pages included), post type, post or page ID, taxonomy term, date range, time of day, day of week, environment and active plugin.
  • Old shortcodes and blocks. [snipfire id="…"] shortcodes and Snipfire blocks in your content keep working with the exported plugin once Snipfire is gone.

The zip also has a readme.txt listing its snippets and where each one runs, and saying that the plugin has no crash protection.

The report, and "Export anyway"

Some settings only work with Snipfire. Before anything is exported, the report lists the snippets that use them:

In the report What happens if you export it anyway
Switched off in Snipfire. It runs whenever the plugin is active
In testing mode. It runs for every visitor
Waits for cookie consent (…). It runs without waiting
Uses conditions the exported plugin can't check: … (Pro rules, rules from other add-ons) It's exported without conditions and runs everywhere
Uses smart tags such as {post_title}… The smart tags stay as written
Calls Snipfire's own functions (e.g. snipfire_secret())… The call fails without Snipfire

These snippets are left out unless you tick Export anyway next to them. Only tick it when running the snippet without that setting is what you want.

Some snippets can't be exported at all, and are listed as Can't be exported:

  • snippets that only run by hand (Only when I click Run) or on a schedule,
  • a CSS snippet with no compiled CSS yet (save it in Snipfire first; SCSS and LESS need Snipfire Pro to compile).

Before you activate it

  • Switch the snippets off in Snipfire first (or deactivate Snipfire) on a site that has both, or they run twice. The exported plugin warns on the Plugins screen while Snipfire is active.
  • A snippet that declares a function or class that already exists is left out by the plugin, instead of stopping the site with a fatal error. The Plugins screen says which one.
  • If both must stay active, activate Snipfire before the exported plugin. Activated the other way round, the exported plugin has already declared the snippet's functions, so Snipfire's own copy fails its check and is switched off.
  • Several exported plugins can be active at the same time.

Changing it later

The exported plugin has no editor and no updates of its own. To change a snippet, edit it in Snipfire and export again with a higher Version, then upload the new zip on the other site (WordPress offers to replace the installed version).

From the command line and the API

wp snipfire export-plugin 12 14 --name="Shop tweaks" --slug=shop-tweaks
wp snipfire export-plugin --folder=WooCommerce --dry-run          # only the report
wp snipfire export-plugin --folder=WooCommerce --include=all --output=/tmp/woo.zip

--include takes the IDs of snippets to export anyway, or all. See WP-CLI and POST /export-plugin.

Something unclear or missing? Tell us