preloader

Code Snippets Plugin: The Right Way to Add Custom PHP to WordPress

Every WordPress developer has been told to “add this code to your functions.php file.” The problem: a PHP error in functions.php breaks your entire site and can lock you out of the admin. Code Snippets solves this by storing custom code in the database, giving each snippet its own scope and error handling, and letting you disable individual snippets without touching theme files.

Why Code Snippets Over functions.php

Three practical reasons:

Error isolation – a syntax error in a Code Snippets snippet can be disabled from the admin without FTP access. A syntax error in functions.php brings down the entire site until you fix it via FTP or cPanel.

Theme independence – code in functions.php disappears when you switch themes. Code Snippets persists across theme changes, child theme switches, and theme updates.

Organisation – as your customisation list grows, functions.php becomes a sprawling file with no clear structure. Code Snippets lets you name, tag, and describe each snippet individually.

Snippet Types

Code Snippets handles more than just PHP. In the Add Snippet screen, select the type:

  • PHP function – runs PHP code on every page load (or within the scope you set)
  • HTML – adds content using shortcodes or automatic insertion points
  • CSS – adds stylesheet rules to the front end or admin
  • JavaScript – adds scripts to the front end

The CSS and JS snippet types are particularly useful – instead of adding custom styles to a child theme’s stylesheet or using a theme customiser, you manage them as named, toggleable snippets.

Snippet Scopes

The scope determines when the snippet runs:

  • Run everywhere – fires on all page loads, front end and admin
  • Run on site front-end only – fires only for visitors, not in admin
  • Run on administration pages only – fires only in wp-admin
  • Run once – fires a single time then automatically deactivates (useful for one-time data migrations)
  • Run on WordPress initialisation – fires very early in the WordPress load cycle, before plugins are loaded

Choosing the right scope matters for performance. A snippet that only needs to run on the front end should not be scoped to run everywhere.

Need a developer to implement this cleanly? Describe what you need and get a free estimate.

Practical Examples

Remove the WordPress version number from your page source (common security hardening step):

remove_action('wp_head', 'wp_generator');

Set this to “Run on site front-end only” scope.

Add a custom dashboard widget for clients:

add_action('wp_dashboard_setup', function() {
    wp_add_dashboard_widget(
        'client_info_widget',
        'Site Information',
        function() {
            echo '<p>For support, contact: support@youragency.com</p>';
        }
    );
});

Set this to “Run on administration pages only” scope.

Change the default number of posts per page for a specific post type:

add_action('pre_get_posts', function($query) {
    if (!is_admin() && $query->is_main_query() && is_post_type_archive('product')) {
        $query->set('posts_per_page', 24);
    }
});

Handling Snippet Errors

If a snippet causes a fatal error, Code Snippets has a built-in safe mode. Access yourdomain.com/wp-admin/snippets.php?snippets-safe-mode=true to deactivate all snippets and recover access. You can also add this to wp-config.php as a permanent escape hatch:

define('CODE_SNIPPETS_SAFE_MODE', true);

Keep Reading

Previous Post Stop Spammers vs WP Armour vs Antispam Bee: Layered Spam Protection Explained Next Post Code Snippets vs WPCodeBox vs Code Snippets Pro: Which Should You Use

Need Help With Your WordPress Site?

If you need help with WordPress fixes, plugin issues, theme customization, or development work, feel free to get in touch.

Get a Free Estimate