Code Snippets Module
The Code Snippets module provides a safe, centralized way to add PHP, HTML, CSS, or JavaScript code to your WordPress site without editing theme or plugin files. Perfect for small customizations, tracking codes, analytics scripts, or extending functionality across the frontend and admin areas.
Getting Started
Enabling the Module
- Navigate to Admin Tools in the WP Extended Pro menu
- Locate the Code Snippets module
- Toggle the switch to enable the module
Once enabled, you can create and manage code snippets from the module settings page.
Supported Snippet Types
- PHP — Server-side logic, WordPress hooks, filters, and custom functions
- HTML — Markup and content snippets
- CSS — Stylesheets and custom styling
- JavaScript — Client-side scripts and functionality
Managing Snippets
List View
The main Code Snippets screen displays all your snippets in a table with the following columns:
- Name — Snippet title
- Type — PHP, HTML, CSS, or JavaScript
- Status — Enabled/Disabled toggle
- Actions — Edit and Delete buttons
Actions available:
- Enable/Disable — Toggle snippet status directly from the list
- Edit — Open the snippet editor
- Delete — Remove the snippet permanently
- Add Snippet — Create a new snippet
Tip: Use the search functionality and column sorting to quickly find specific snippets.
Editor View
When creating or editing a snippet, configure the following fields:
Basic Information
- Snippet Name — A descriptive title for your snippet
- Description — Optional notes about the snippet’s purpose
- Snippet Type — Select PHP, HTML, CSS, or JavaScript
- Snippet Code — Code editor with syntax highlighting for your chosen type
Where to Run
The available run locations vary by snippet type:
PHP Snippets:
- Everywhere — Runs on both frontend and admin pages
- Admin Only — Executes only in WordPress admin area
- Frontend Only — Runs only on public-facing pages
HTML Snippets:
- Everywhere — Frontend and admin
- Frontend Header — In the
<head> section of frontend pages
- Frontend Body Open — Immediately after the opening
<body> tag
- Frontend Footer — Before the closing
</body> tag
- Admin Header — In the admin
<head> section
- Admin Footer — Before the closing
</body> tag in admin
CSS Snippets:
- Everywhere — Frontend and admin
- Frontend Header — In frontend
<head> section
- Frontend Footer — Before closing
</body> on frontend
- Admin Header — In admin
<head> section
- Admin Footer — Before closing
</body> in admin
JavaScript Snippets:
- Everywhere — Frontend and admin
- Frontend Header — In frontend
<head> section
- Frontend Footer — Before closing
</body> on frontend
- Admin Header — In admin
<head> section
- Admin Footer — Before closing
</body> in admin
Controls
- Enable Snippet — Toggle to activate or deactivate the snippet
- Save/Update — Save changes and return to list view
- Back to List — Cancel editing and return to list view
Safe Mode
Safe Mode temporarily disables all code snippets, which is essential if a snippet causes issues that prevent you from accessing the WordPress admin.
Enabling Safe Mode
You can enable Safe Mode using any of these methods:
- URL Parameter (Admin Users) — Add
?wpextended_safe_mode=1 to any WordPress URL while logged in as an administrator. This sets a cookie that persists for 30 days.
- WordPress Constant — Add the following to your
wp-config.php file for a persistent, site-wide setting:
define('WP_EXTENDED_SAFE_MODE', true);
- Cookie — Manually set the
wpextended_safe_mode cookie to 1 (useful for non-admin users or programmatic control).
Disabling Safe Mode
When Safe Mode is active, a warning notice appears at the top of your WordPress admin dashboard with a link to disable Safe Mode. Click the link to turn off Safe Mode and re-enable your snippets.
Alternatively, you can disable Safe Mode by:
- Removing the constant from
wp-config.php (if using that method)
- Clearing the
wpextended_safe_mode cookie
- Visiting any WordPress URL with
?wpextended_safe_mode=0&_wpnonce=[nonce] (admin only)
Note: When Safe Mode is active, all code snippets are disabled regardless of their individual enabled status. This ensures you can always access your admin to fix problematic snippets.
Best Practices
- Use the right type — PHP for logic and WordPress hooks; HTML/CSS/JS for presentation and client-side functionality
- Scope appropriately — Choose Frontend vs Admin run locations to avoid unexpected interference
- Keep snippets focused — Prefer plugins or themes for large, complex features
- Test first — Always test snippets on a staging site before enabling on production
- Use Frontend Body Open — For HTML that must appear immediately after the opening
<body> tag
- Document your snippets — Use the description field to note what each snippet does and why it exists
Troubleshooting
Snippet Not Running
- Check Status — Ensure the snippet is enabled (Status column shows “Enabled”)
- Verify Run Location — Confirm the “Where to Run” setting matches your target area (Frontend/Admin; header/body/footer)
- Check Syntax — Verify your code has no syntax errors
- Clear Caches — Purge any caching plugins or server-side caches
PHP Snippet Caused a Problem
If a PHP snippet prevents you from accessing the admin:
- Enable Safe Mode using one of the methods above
- Once in the admin, you’ll see a Safe Mode notice
- Navigate to Code Snippets and disable or edit the problematic snippet
- Disable Safe Mode using the link in the admin notice
HTML/JS/CSS Output Missing
- Remove wrappers — The module automatically wraps JavaScript and CSS in
<script> and <style> tags; don’t include these in your code
- Check caching — Clear all caches (browser, WordPress, CDN, minification plugins)
- Verify run location — Ensure the snippet is set to run in the correct location (header/footer/body)
- Check for conflicts — Temporarily disable other plugins to identify conflicts
Admin/Editor Conflicts
- Change run location — Switch the snippet to “Frontend Only” or “Admin Only” to isolate the issue
- Temporarily disable — Turn off the snippet to restore default behavior and identify the problem
- Check for JavaScript errors — Use browser developer tools to identify conflicts
Technical Notes
- No file modifications — Snippets affect only runtime output; theme and plugin files remain unchanged
- Hook mapping — Non-PHP snippets are automatically output on the appropriate WordPress hooks:
- Frontend:
wp_head, wp_body_open, wp_footer
- Admin:
admin_head, admin_footer
- Module-specific save — The Code Snippets module uses its own Save/Update buttons; the global “Save changes” button is hidden for this module
- Security — All snippets are stored in
wp-content/wpextended-snippets/ and validated before execution
Developer Hooks
The module provides the following filters for developers:
wpextended/code-snippets/dependencies — Display dependency notices in the module UI
wpextended/code-snippets/register_settings — Extend the module settings and UI
wpextended/code-snippets/show_save_changes_button — Control global save button visibility for this module
Use Cases
This module is perfect for:
- Quick customizations — Small tweaks without modifying theme files
- Tracking codes — Google Analytics, Facebook Pixel, and other tracking scripts
- Custom functions — Small PHP functions and WordPress hooks
- Site-wide styles — Global CSS customizations
- Client sites — Safe, centralized snippet management for client projects
- A/B testing — Quick script injection for testing purposes