Key points
- Never edit the functions.php of a theme you didn’t write: its next update wipes your code.
- Simple rule: anything about how the site looks belongs in the (child) theme; anything that must survive a theme change belongs in a site plugin.
- Use mu-plugins for code that must never be switched off, always lint before deploying, and disable the built-in file editor.
“Add this snippet to your theme’s functions.php file.” Half the WordPress tutorials out there say it, and it is behind a lot of the problems we find when we take over a site: code lost after a theme update, features that vanish during a redesign, or a white screen after someone edited a file live on production. The code itself usually works. The location is the problem. Here is where custom code should go depending on what it does, and how to organise it so it stays maintainable.
Five places to put code, and where each falls short
WordPress loads custom PHP from several places, and each behaves differently when the theme is updated or replaced:
- The parent theme’s functions.php: off limits if the theme comes from a vendor or the official directory. It is overwritten on every update and your code disappears without warning.
- A child theme’s functions.php: survives parent theme updates. But everything in it is gone the day you switch themes.
- A custom site plugin: a folder in
wp-content/pluginswith one PHP file. Theme-independent, can be activated and deactivated, and lives in version control. - A mu-plugin (“must-use”): a file in
wp-content/mu-plugins, loaded automatically before regular plugins and impossible to deactivate from the dashboard. - A snippets plugin: handy without file access, but the code lives in the database, outside version control, and errors can be harder to trace.
If you built the theme yourself for a bespoke site, its functions.php is a perfectly reasonable home for display-related code. The file isn’t the issue; mixing non-visual logic into the theme is.
The deciding question: what happens if you change themes?
Ask it for every piece of code. If the answer is “it must keep working”, it doesn’t belong in the theme. A few real examples from our projects:
- a custom post type (case studies, job offers, properties): plugin, otherwise the content becomes unreachable in the dashboard after a theme switch;
- a shortcode used inside posts: plugin, or raw
[my_shortcode]text shows up across hundreds of pages; - a redirect, a security tweak, a CRM integration: plugin;
- registering menus, design-specific image sizes, block styles: theme;
- blocking search engines on staging: mu-plugin, so nobody can switch it off by accident.
On our sites we create a “site settings” plugin at the very start of every project. It holds everything about how the site works; the theme holds only how it looks. A visual redesign then means touching the theme alone.
Building a site plugin in two minutes
A WordPress plugin is just a PHP file with a comment header. Create wp-content/plugins/site-settings, add a file with the same name, and activate it in the dashboard:
<?php
/**
* Plugin Name: Site settings (Studio)
* Description: Custom code for this site: hooks, small helpers, tweaks.
* Version: 1.0.0
* Requires PHP: 8.1
*/
defined( 'ABSPATH' ) || exit;
// Remove the WordPress version from meta tags
remove_action( 'wp_head', 'wp_generator' );
// Longer automatic excerpts
add_filter( 'excerpt_length', fn() => 40 );
The defined( 'ABSPATH' ) || exit; line stops the file from running if someone requests its URL directly. Requires PHP prevents activation on an outdated server, which would otherwise end in a fatal error as soon as you use modern syntax such as arrow functions or enums.
Once the file grows, split it by feature instead of letting it reach hundreds of lines:
wp-content/plugins/site-settings/
├── site-settings.php # header + file loading
└── inc/
├── admin.php # admin customisations
├── content.php # post types, taxonomies
└── integrations.php # forms, external APIs
// In site-settings.php, after the header
foreach ( [ 'admin', 'content', 'integrations' ] as $file ) {
require_once __DIR__ . "/inc/{$file}.php";
}
That folder goes into Git like any other project, which is the decisive advantage over snippet plugins: you know who changed what and when, and you can roll back.
mu-plugins: for code that must never be turned off
PHP files placed directly in wp-content/mu-plugins load on every request, no activation needed. They only show up under a “Must-Use” tab in the plugins list, with no deactivate link. That is exactly what you want for environment or security code:
<?php
// wp-content/mu-plugins/environment.php
// Loaded automatically, cannot be deactivated from the dashboard.
defined( 'ABSPATH' ) || exit;
// Block indexing everywhere except production
if ( wp_get_environment_type() !== 'production' ) {
add_filter( 'wp_robots', 'wp_robots_no_robots' );
}
Two quirks to know. WordPress only loads PHP files at the root of that folder, not in subfolders, so a larger structure needs a small root file that does the require calls. And mu-plugins load before regular plugins: don’t call another plugin’s functions directly at load time; hook into plugins_loaded instead.
We keep large business logic out of them. A mu-plugin can’t be switched off from the dashboard when something goes wrong; you need file access. Keep them for code that is short, stable and critical.
Adding code without taking the site down
One typo in a PHP file WordPress loads is a fatal error. Since 5.2, WordPress catches these and emails the admin a recovery-mode link, which only helps if emails actually go out and someone reads them. Better to avoid the situation altogether:
// wp-config.php : disable the built-in file editor
define( 'DISALLOW_FILE_EDIT', true );
// Before deploying: check the syntax
// php -l wp-content/plugins/site-settings/site-settings.php
// After a fatal error: deactivate the plugin without dashboard access
// wp plugin deactivate site-settings --skip-plugins
- turn off the dashboard file editor: editing PHP live in the browser, with no backup and no undo, is the most common cause of white screens we are called in for;
- test on staging or locally, then deploy;
- prefix your functions (
studio_, say) or use a namespace so you don’t collide with a plugin that declares a function with the same name; - when you copy a tutorial snippet, check its date and the functions it calls: plenty of old snippets rely on deprecated functions.
If the error has already happened and the dashboard is unreachable, renaming the plugin’s folder over SFTP deactivates it too. For a mu-plugin, rename or move the file itself.
FAQ
Isn’t a child theme’s functions.php good enough?
For small display tweaks, yes. For anything that has to survive a theme change (post types, shortcodes, integrations), a site plugin is still the better home, even if the site only uses a child theme today.
Should I avoid snippet plugins altogether?
They are a lifesaver without file access. On a site maintained by a team we prefer code in versioned files: it can be reviewed, tested and deployed like the rest of the project.
Years of code piled up in your theme’s functions.php, or a redesign coming up? We reorganise your site’s custom code so it survives updates and theme changes.



