Scope controls when and where each snippet runs or is loaded.
PHP scope #
| Field | Description |
|---|---|
| Hook | The WordPress action hook to run the snippet on. Common values: init, wp_loaded, template_redirect. Default: init. |
| Priority | Hook priority. Lower numbers run earlier. Default: 10. |
| Context | Which requests to run on. Check one or more contexts; the snippet runs when the request matches any of them. Leave all unchecked to run everywhere. See PHP contexts below. |
| Run once then deactivate | Checkbox, off by default. When checked, the snippet runs once and is then set to Inactive (its file is kept). Useful for one-time migration scripts. |
If the configured hook has already fired by the time GOCSM registers the snippet, GOCSM executes the snippet immediately for that request after checking the configured context.
PHP contexts #
GOCSM checks the context when the snippet’s hook fires.
| Context | Runs on |
|---|---|
| Frontend pages | Public page requests only: not admin, AJAX, REST, cron, WP-CLI, XML-RPC or login pages. |
| Admin pages | wp-admin pages, including admin-post.php form handlers. Not AJAX. |
| AJAX requests | admin-ajax.php, for logged-in and logged-out visitors. |
| Login pages | wp-login.php, plus whatever page the login, registration and lost-password links point to, so a plugin that moves the login page is covered. That page is recognised from init onwards; on earlier hooks only wp-login.php is. |
| REST requests | REST API requests, both /wp-json/… and ?rest_route=…. |
| Cron | wp-cron.php and wp cron event run. |
| WP-CLI | WP-CLI commands. |
To cover several kinds of request, check several contexts. For example, a snippet that answers both AJAX and REST calls checks AJAX requests and REST requests. XML-RPC requests run only snippets with no context checked (everywhere).
REST routes another plugin calls from inside a page request with rest_do_request() are not REST requests, so a snippet limited to REST requests has not loaded then. If other code calls your snippet’s routes that way, add the contexts it is called from, or leave the context unchecked.
Through the API, context is everywhere, one context name, or a list of names: frontend, admin, ajax, login, rest, cron, cli. Unknown names are rejected.
JS scope #
| Field | Description |
|---|---|
| Context | Everywhere, Frontend only, Admin only, or Login page only. |
| Location | Head (default) or Footer. |
| Page targeting | Restrict loading to all pages, URL contains, URL regex, post types, or page templates. See Page targeting below. |
| Dependencies | Comma-separated script handles that must load before this one (e.g. jquery). |
| Version | Script version string. Leave blank to use the generated file’s modification time, which changes whenever the code or a config value changes. |
| Strategy | defer (default), async, or Blocking. Invalid values fall back to defer. |
CSS scope #
| Field | Description |
|---|---|
| Context | Everywhere, Frontend only, Admin only, or Login page only. |
| Page targeting | Same targeting options as JS. |
| Media query | CSS media attribute. Default: all. Example: screen and (max-width: 768px). |
| Dependencies | Comma-separated stylesheet handles that must load before this one. |
| Version | Stylesheet version string. Leave blank to use the generated file’s modification time, which changes whenever the code or a config value changes. |
HTML scope #
| Field | Description |
|---|---|
| Location | Where the markup prints. Front end: Head, Start of body, or Footer (default). Admin: Admin head or Admin footer. Login page: Login head or Login footer. Shortcode only prints the snippet only where its shortcode is placed. Start of body needs a theme that calls wp_body_open(). |
| Priority | Hook priority for the location. Lower numbers print earlier. Default: 10. |
| Page targeting | Same targeting options as JS. Applies to the location only. |
The shortcode [gocsm_snippet id="123"] prints an active HTML snippet wherever it is placed, whatever its location setting. The HTML Snippet block does the same in the block editor: add the block and choose the snippet. The editor shows the snippet’s name instead of a preview, because HTML snippets often contain scripts that should not run while you edit. Page targeting does not apply to the shortcode or block, and an inactive snippet prints nothing.
Page targeting #
JS, CSS, and HTML snippets can be limited to specific pages within their context:
| Option | How it matches |
|---|---|
| All pages (within context) | Every page in the chosen context. |
| URL contains | One entry per line. Matches when the requested URL path contains any entry (not case-sensitive). |
| URL regex | A regular expression matched against the requested URL path. Enter it without delimiters, e.g. ^/blog/. The pattern is checked when you save. |
| Post types | Comma-separated post types, e.g. post, page. Matches single-item views of those types only. |
| Page templates | Comma-separated template filenames, e.g. template-landing.php. |
Post type and page template targeting never match on admin screens.
Stuck? Ask in GravityOps Help #
Search past answers or post your question, tagged "snippet-manager".
GravityOps Weekly