Skip to content

Configure Code Snippets

Configure the Code Snippets module

For the marketing overview of this module, see /modules/code-snippets. This page covers the practical setup — Quickstart, common workflows, settings reference, and the developer surface (REST + WP-CLI).

This module is free at full functionality in Asteris for WordPress Free on WordPress.org. No Pro upsell. The full feature surface (PHP / JS / CSS, conditional loading, safe-mode auto-disable, library, import/export) is in the free version.


Quickstart (5 minutes — add your first snippet)

1. Activate the module

WP Admin → Asteris → Modules → toggle Code Snippets to ON.

The Snippets submenu appears.

2. Add your first snippet — a safe one

Pick a low-risk first snippet to test the workflow. Asteris → Snippets → Add New:

  • TitleCustom admin footer text
  • Type — PHP
  • Code
add_filter( 'admin_footer_text', function() {
return 'Custom footer · maintained by Acme · 2026';
});
  • Run location — Admin only
  • Conditional loading — leave defaults (load on every admin page)
  • Active

Click Save.

3. Verify it’s working

Open any WP Admin page. Scroll to the bottom — the footer text should read “Custom footer · maintained by Acme · 2026” (or whatever you set in the snippet).

If it doesn’t:

  1. Check the snippet is marked Active (toggle next to its row in the snippets list)
  2. Check the snippet’s Run location matches where you’re testing (admin vs front-end)
  3. Check Asteris → Snippets → Activity Log for errors (caught fatal errors are logged here)

4. Add a JavaScript snippet

Asteris → Snippets → Add New:

  • TitleCustom console message on every page
  • Type — JavaScript
  • Code
console.log('Asteris snippets module is working');
  • Run location — Front-end only
  • Conditional loading — Default (load on every front-end page)
  • Active

Save. Open your front-end in a private window with DevTools Console open. You should see the message.

5. Try the safe-mode protection

This is the differentiator. Let’s intentionally cause a fatal error and confirm the site doesn’t go down.

Asteris → Snippets → Add New:

  • TitleTest safe-mode (intentionally broken)
  • Type — PHP
  • Code
this_function_does_not_exist_anywhere();
  • Run location — Front-end only
  • Active

Save.

Open your front-end in a private window. Your site should load normally — not show a white screen.

In Asteris → Snippets, the broken snippet should now be marked Auto-disabled (fatal error). Click it to see the error detail:

  • Error type: E_ERROR
  • Message: Call to undefined function this_function_does_not_exist_anywhere()
  • File + line: the snippet’s body
  • Stack trace: full PHP stack at the point of failure

The safe-mode caught the fatal, deactivated the snippet, logged the error, and let your site continue running. Delete this test snippet now.

6. Browse the snippet library

Asteris → Snippets → Library

Asteris ships ~50 pre-built snippets for common tasks. Browse the categories:

  • WP cleanup — Remove version number from headers, disable WP version generator meta, etc.
  • Performance — Disable emoji-detection script, defer specific scripts
  • Security — Disable XML-RPC (if not using the Security module’s toggle), enforce SSL admin
  • Editor tweaks — Disable Gutenberg on specific post types, custom block colours
  • Customisation — Custom admin login logo, custom dashboard widget
  • WC adjustments (when Asteris for WooCommerce is installed)

Click any library snippet → Insert → you get a pre-filled new snippet form. Edit, set conditional loading rules, save.

7. Set up conditional loading

For snippets that should only run on specific URLs / post types / user roles, configure conditional loading:

Asteris → Snippets → edit a snippet → Conditional Loading

Rule types:

  • By location — Front-end / Admin / Both / Specific URL pattern (regex)
  • By post type — only on post, only on page, only on product, only on custom post types
  • By user role — administrator / editor / author / subscriber / not-logged-in
  • By logged-in state — only logged-in / only logged-out / any
  • By taxonomy — only when in category X, only when tagged Y
  • By URL pattern — regex match against REQUEST_URI

Combine multiple rules with AND or OR. Example: “Run on the front-end AND only for logged-in administrators on URLs matching /admin-tools/.*”.

8. Done — the baseline is working

You now have:

  • ✓ Snippets module active
  • ✓ A working PHP snippet (admin footer)
  • ✓ A working JS snippet (console message)
  • ✓ Verified safe-mode protection (broken snippet didn’t crash the site)
  • ✓ Library available for common-task starters
  • ✓ Conditional loading rules understood

Add real snippets as needs arise. Workflows below cover import/export, debugging, and specific patterns.


Common workflows

Migrate from WPCode / Code Snippets to Asteris

Both WPCode and the Code Snippets plugin export their snippets as JSON. Asteris imports both formats:

On the source plugin:

  1. WPCode → Tools → Export → check all snippets → Export
  2. Code Snippets plugin → Snippets → Import/Export → Export → choose all → Export

Save the JSON file.

In Asteris:

  1. Asteris → Snippets → Tools → Import
  2. Choose file — paste the JSON file
  3. Conflict mode — Skip if exists / Update if exists / Add new (rename if conflict)
  4. Click Import

Asteris parses both WPCode and Code Snippets JSON formats. Each imported snippet is created as inactive by default — review them before activating.

After import, deactivate the source plugin. Don’t delete it for 30 days — your imported snippets stay in Asteris regardless, but having the source plugin available means you can re-export if anything’s missing.

Add a snippet to disable Gutenberg on specific post types

A common request — you have custom post types where the block editor isn’t right:

Asteris → Snippets → Add New:

  • TitleDisable Gutenberg on Custom Post Types
  • Type — PHP
  • Code
add_filter( 'use_block_editor_for_post_type', function( $use_block_editor, $post_type ) {
if ( in_array( $post_type, [ 'product', 'my_custom_type' ], true ) ) {
return false;
}
return $use_block_editor;
}, 10, 2 );
  • Run location — Admin only
  • Run priority — 10 (default)
  • Active

Save. Edit a post of the listed CPT — should now use the classic editor.

Add a CSS-only snippet for admin styling

Asteris → Snippets → Add New:

  • Type — CSS
  • Code
#wpadminbar { background: #1a1a1a !important; }
#wpadminbar .ab-item:hover { background: #3D5AFE !important; }
  • Run location — Admin only
  • Active

Save. Refresh any admin page — the admin bar should now be black with indigo hover.

CSS snippets are output in <style> tags in the appropriate <head> location.

Run a snippet only on logged-in users

For features that should only fire when someone’s logged in:

Asteris → Snippets → edit snippet → Conditional Loading

  • By logged-in state — Only logged-in
  • By user role (optional further restriction) — administrator / editor / etc.

Save. The snippet now runs only when a logged-in user (matching the role filter, if set) is on the page.

Restore an auto-disabled snippet after fixing the error

When a snippet fataled and was auto-disabled:

  1. Asteris → Snippets → find the auto-disabled snippet (red status indicator)
  2. Click to expand → review the error
  3. Click Edit → fix the code
  4. Save → the snippet is re-validated (syntax check) on save
  5. If syntax-clean → toggle Active
  6. The next page load that hits the snippet’s hook → it runs again

If it fatals again, Asteris auto-disables again. Repeat the cycle until the bug is fixed.

Set up a snippet to run only on a specific URL

For per-page customisations:

Conditional Loading → By URL pattern

Regex pattern matches against REQUEST_URI. Examples:

  • ^/landing-page/$ — exactly the landing page (single match)
  • ^/blog/.* — anything under /blog/
  • ^/(en|fr|de)/.* — language-prefixed URLs

The snippet runs only when the regex matches the request URI.

Disable a snippet temporarily (without deleting)

Sometimes you want to test if the site behaves differently without a specific snippet:

  1. Asteris → Snippets → find the snippet
  2. Toggle Active → Inactive
  3. Refresh / re-test
  4. Toggle back to Active when done

Snippet code is preserved; it just isn’t loaded.

Bulk export all snippets (backup)

Before making any large change to your snippets (mass edit, library overhaul):

Terminal window
wp asteris snippets export --file=snippets-backup-2026-06-04.json

Saves all snippets + settings to a portable JSON. Restore with:

Terminal window
wp asteris snippets import --file=snippets-backup-2026-06-04.json

Useful both as a safety net + as a way to migrate snippets between sites.

Find which snippet is causing a problem

If the site is misbehaving + you suspect a recent snippet:

  1. Asteris → Snippets → Activity Log → see recent snippet changes
  2. Asteris → Snippets → sort by Last modified → find recently-edited snippets
  3. Temporarily deactivate the most-recent change
  4. Test
  5. Reactivate, repeat with the next suspect

For a more systematic isolation:

Terminal window
# Deactivate ALL snippets temporarily
wp asteris snippets bulk-deactivate
# Test the site — problem gone? It was a snippet.
# Now reactivate one at a time:
wp asteris snippets activate --id=<id>
# Test after each. Stop when you find the bad one.

When debugging is complete:

Terminal window
wp asteris snippets bulk-activate

(Reactivates everything that was previously active.)


Settings reference

Snippet types

  • PHP — runs at WordPress hooks
  • JavaScript — enqueued on front-end / admin / both
  • CSS — output in <style> tags
  • HTML head — injected into <head>
  • HTML body — injected before </body>
  • Shortcode — registered as [asteris_snippet_<id>] for use in posts/pages

Conditional loading

  • By location — Front-end / Admin / Both / Specific URL regex
  • By post type — multiple post types selectable
  • By user role — multiple roles selectable
  • By logged-in state
  • By taxonomy — category, tag, or custom taxonomy
  • By URL pattern — regex against REQUEST_URI
  • Combine rules — AND / OR logic

Safety

  • Safe-mode auto-disable — catches E_ERROR, E_PARSE, E_CORE_ERROR
  • Pre-save syntax validationphp -l equivalent for PHP snippets
  • Activity Log integration — all snippet activations + auto-disables logged

Library

  • ~50 built-in snippets across WP cleanup / performance / security / editor / customisation
  • One-click insertion (creates a new draft snippet you can edit before saving)

Import / Export

  • JSON export — all snippets + settings
  • JSON import — same format
  • WPCode export compatibility — imports WPCode Pro JSON exports
  • Code Snippets export compatibility — imports the Code Snippets plugin’s JSON

REST API

# Snippets
GET /wp-json/asteris/v1/snippets
GET /wp-json/asteris/v1/snippets/<id>
POST /wp-json/asteris/v1/snippets # create
PUT /wp-json/asteris/v1/snippets/<id> # update
DELETE /wp-json/asteris/v1/snippets/<id>
# Activation
POST /wp-json/asteris/v1/snippets/<id>/activate
POST /wp-json/asteris/v1/snippets/<id>/deactivate
POST /wp-json/asteris/v1/snippets/bulk-activate
POST /wp-json/asteris/v1/snippets/bulk-deactivate
# Import / Export
POST /wp-json/asteris/v1/snippets/import # body: { file: <base64-json> }
GET /wp-json/asteris/v1/snippets/export
# Safe-mode
GET /wp-json/asteris/v1/snippets/auto-disabled
POST /wp-json/asteris/v1/snippets/safe-mode/reset

All capability-checked (manage_options).


WP-CLI

Terminal window
# Snippets
wp asteris snippets list
wp asteris snippets info --id=<id>
wp asteris snippets create --title="<title>" --type=php --code="<code>"
wp asteris snippets update --id=<id> --code="<new-code>"
wp asteris snippets delete --id=<id>
# Activation
wp asteris snippets activate --id=<id>
wp asteris snippets deactivate --id=<id>
wp asteris snippets bulk-activate
wp asteris snippets bulk-deactivate
# Import / Export
wp asteris snippets export --file=snippets.json
wp asteris snippets import --file=snippets.json
# Safe-mode
wp asteris snippets auto-disabled-list
wp asteris snippets safe-mode reset

See also