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:
- Title —
Custom 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:
- Check the snippet is marked Active (toggle next to its row in the snippets list)
- Check the snippet’s Run location matches where you’re testing (admin vs front-end)
- Check Asteris → Snippets → Activity Log for errors (caught fatal errors are logged here)
4. Add a JavaScript snippet
Asteris → Snippets → Add New:
- Title —
Custom 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:
- Title —
Test 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 onpage, only onproduct, 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:
- WPCode → Tools → Export → check all snippets → Export
- Code Snippets plugin → Snippets → Import/Export → Export → choose all → Export
Save the JSON file.
In Asteris:
- Asteris → Snippets → Tools → Import
- Choose file — paste the JSON file
- Conflict mode — Skip if exists / Update if exists / Add new (rename if conflict)
- 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:
- Title —
Disable 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:
- Asteris → Snippets → find the auto-disabled snippet (red status indicator)
- Click to expand → review the error
- Click Edit → fix the code
- Save → the snippet is re-validated (syntax check) on save
- If syntax-clean → toggle Active
- 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:
- Asteris → Snippets → find the snippet
- Toggle Active → Inactive
- Refresh / re-test
- 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):
wp asteris snippets export --file=snippets-backup-2026-06-04.jsonSaves all snippets + settings to a portable JSON. Restore with:
wp asteris snippets import --file=snippets-backup-2026-06-04.jsonUseful 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:
- Asteris → Snippets → Activity Log → see recent snippet changes
- Asteris → Snippets → sort by Last modified → find recently-edited snippets
- Temporarily deactivate the most-recent change
- Test
- Reactivate, repeat with the next suspect
For a more systematic isolation:
# Deactivate ALL snippets temporarilywp 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:
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 validation —
php -lequivalent 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
# SnippetsGET /wp-json/asteris/v1/snippetsGET /wp-json/asteris/v1/snippets/<id>POST /wp-json/asteris/v1/snippets # createPUT /wp-json/asteris/v1/snippets/<id> # updateDELETE /wp-json/asteris/v1/snippets/<id>
# ActivationPOST /wp-json/asteris/v1/snippets/<id>/activatePOST /wp-json/asteris/v1/snippets/<id>/deactivatePOST /wp-json/asteris/v1/snippets/bulk-activatePOST /wp-json/asteris/v1/snippets/bulk-deactivate
# Import / ExportPOST /wp-json/asteris/v1/snippets/import # body: { file: <base64-json> }GET /wp-json/asteris/v1/snippets/export
# Safe-modeGET /wp-json/asteris/v1/snippets/auto-disabledPOST /wp-json/asteris/v1/snippets/safe-mode/resetAll capability-checked (manage_options).
WP-CLI
# Snippetswp asteris snippets listwp 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>
# Activationwp asteris snippets activate --id=<id>wp asteris snippets deactivate --id=<id>wp asteris snippets bulk-activatewp asteris snippets bulk-deactivate
# Import / Exportwp asteris snippets export --file=snippets.jsonwp asteris snippets import --file=snippets.json
# Safe-modewp asteris snippets auto-disabled-listwp asteris snippets safe-mode resetSee also
- Asteris for WordPress Free — Code Snippets is the only module included at full functionality in the free version
- Recovery / safe mode — when a snippet breaks the admin (recovery via WP-CLI / SFTP)
- Activity Log + Site Health module — snippet activations + safe-mode events appear in the unified log