Docs / Writing snippets

Snippet types and run locations

The four types, every run location each one offers, and how priority orders snippets on the same hook.

All documentation

A snippet has a type and a run location. The type says what language you are writing and decides which locations are available. The location says where in the WordPress request your code is attached.

The four types

TypeEditor languageWhat it is for
FunctionsPHPServer-side code, as in functions.php. Hooks, filters, classes, integrations.
ContentPHP + HTMLOutput. Tracking tags, banners, markup around post content, shortcodes.
StylesCSSStyling the front end, the admin, or the block editor.
ScriptsJavaScriptThird-party embeds and small bits of behaviour.

Each has its own page with the details: Functions, Content, Styles, Scripts.

The type is fixed once a snippet is created. To change it, create a new snippet and delete the old one.

Run locations

LocationAvailable inWhat it does
Run EverywhereFunctionsRuns on both the front end and the admin
Admin OnlyFunctionsRuns only under /wp-admin/
Frontend OnlyFunctionsRuns only on the front end
Site Wide HeaderContent, ScriptsPrints inside <head> on the front end (wp_head)
Site Wide Body OpenContentPrints straight after the opening <body> tag (wp_body_open)
Site Wide FooterContent, ScriptsPrints before </body> on the front end (wp_footer)
Before ContentContentPrepends to the content of a single post, page or CPT
After ContentContentAppends to the content of a single post, page or CPT
ShortcodeContentNothing runs until you place [fluent_snippet id="…"]
Admin Area HeaderScriptsPrints in the admin <head> (admin_head)
Admin Area FooterScriptsPrints before </body> in the admin (admin_footer)
FrontendStylesAdds the CSS on the front end
BackendStylesAdds the CSS in /wp-admin/
Both Backend and FrontendStylesAdds the CSS in both places
NOTE

Site Wide Body Open depends on your theme calling wp_body_open(), which themes have been expected to do since WordPress 5.2. A theme that skips it will not print body-open snippets. If yours does not, use Site Wide Header instead.

Priority

Every snippet has a Priority field. It is passed straight through as the WordPress hook priority, so the usual rules apply: lower numbers run earlier, the default is 10, and the minimum accepted here is 1.

Priority only matters between snippets sharing the same hook, and between your snippet and other code on that hook. Two examples:

  • Two Site Wide Header Content snippets, one that defines a dataLayer and one that reads it. Give the definition priority 5 and the reader priority 10.
  • A Frontend Only Functions snippet that must run before a theme’s own callback registered at priority 10. Set it to 5.

Anything that is not a number, or is below 1, falls back to 10.

Draft and published

Every snippet is either published (running) or draft (saved, not running). Draft snippets are parsed and indexed but never loaded, so a draft costs nothing at runtime.

New snippets are saved as drafts by default. Turn on Publish new snippets straight away in Settings to have them published straight away instead. Imported snippets always arrive as drafts, whatever that setting says.

Toggle the status from the switch in the snippet list, or from the Activate / Deactivate button on the editor screen.

A snippet that has been quarantined after a fatal error shows as Paused with an ERROR badge, and its toggle is replaced until you fix and re-save it.

Where the settings are stored

Type, run location, priority, status, group, tags and conditional logic all live in a comment block at the top of the snippet’s own .php file, not in the database. That is what makes snippets portable between sites. The format is documented in File format and headers.