View Categories

Activating and Deactivating Snippets

What happens when you activate a snippet #

  1. GOCSM checks the snippet’s requirements (minimum PHP and WordPress versions, and required active plugins) when it declares any. If a requirement is not met, activation stops and the snippet is set to Error with a message such as “Requires active plugin: …”. Requirements are checked only at activation.
  2. GOCSM reads the snippet’s source code and any saved config values from the database.
  3. Config values are injected into the config block (replacing the default values).
  4. The combined code is validated. PHP uses token_get_all() with TOKEN_PARSE. JavaScript is checked for balanced brackets only. CSS is checked for balanced braces, and @import is rejected. If validation fails, activation is aborted and the error is shown — nothing is written to disk.
  5. The validated code is written atomically: first to a temporary file, then renamed to the final path. This prevents a partially-written file from being required. If PHP file generation fails and eval fallback is enabled, the snippet can activate without a generated file path.
  6. PHP file permissions are set to 0444 (read-only) to prevent post-write modification without elevated privileges. JS and CSS files are written with 0644.
  7. OPcache is invalidated for PHP generated file paths so PHP picks up the new bytecode immediately.
  8. The generated PHP file hash is stored for load-time integrity checks.
  9. The snippet’s status is set to active in the database.

On the next WordPress request, the Loader includes the file (PHP) or enqueues it (JS/CSS).

Where to activate and deactivate #

  • Row actions — Activate or Deactivate under the snippet’s name in the snippets list.
  • Edit screen — the Activate or Deactivate button in the Status card.
  • Bulk actions — select snippets in the list, choose Activate, Deactivate, or Delete from Bulk actions, and click Apply. Bulk Delete skips GitHub-sourced snippets, which are managed by their collection.

Snippets in the Error status show Activate, so you can fix the problem and activate again directly. You can also deactivate them with the bulk Deactivate action.

Snippets that call exit or die while loading #

A PHP snippet that calls exit or die outside a function ends every request that loads it, with no output and no error. That includes front-end pages, WP-CLI, cron and REST. PHP cannot catch exit, so GOCSM cannot step in once the snippet is running. A common example is if ( ! is_admin() ) { exit; } at the top of a snippet meant for wp-admin only. Use return instead, which stops only the snippet, or set the snippet’s context to Admin.

When you activate such a snippet from a row action or the edit screen, GOCSM lists the lines involved and asks you to confirm first. Saving the snippet shows the same warning. Bulk activation, imports and the abilities API activate the snippet without asking. The standard if ( ! defined( 'ABSPATH' ) ) { exit; } guard and exits inside functions, methods and closures are not flagged.

What happens when you deactivate a snippet #

The snippet’s status is set to inactive. Config values are preserved in the database for when you reactivate.

Deactivation does not delete the generated file or clear the stored file path. Standalone custom snippets are deleted from disk only when you delete the snippet. GitHub-sourced snippets are removed by deleting or updating their collection.

Generated file locations #

Type Path
PHP wp-content/uploads/gocsm/snippets/{collection}__{slug}.php
JS wp-content/uploads/gocsm/js/{collection}__{slug}.js
CSS wp-content/uploads/gocsm/css/{collection}__{slug}.css

For custom snippets, {collection} is custom and {slug} is a random identifier GOCSM assigns when the snippet is created (for example custom__custom-a1b2c3d4.php), not the snippet’s name. For GitHub snippets, {collection} is the collection’s slug in GOCSM, based on its display name or user/repo, and {slug} is the snippet’s manifest id. PHP slugs are shortened to 80 characters.

List actions and Trash #

Click the status button to activate or deactivate a snippet. Preview shows its source without running it. Duplicate creates an inactive custom copy with its configuration, scope, requirements, tags and folder.

Trash deactivates a custom snippet while keeping its source, configuration, tags and revisions. Open Trash to restore it; restored snippets remain inactive until explicitly activated. Delete permanently removes it and its history after confirmation. GitHub snippets remain managed through their collection.