Customizing WordPress is super easy. On the one hand, there’s a huge ecosystem of plugins that will transform your website and add new and awesome functionalities. On the other hand, there’s plenty of themes that will completely change and revamp the look and feel of your site. But you can go far beyond that if you’re willing to tinker WordPress‘ source code. Some examples include customizing WordPress login screen or supporting additional file types, but there’s a lot more.
Cool, huh? If you’re new to WordPress or you aren’t a code expert, the thought of modifying its code can be a daunting one. But that’s normal! Let’s see (some of) the problems of tinkering code and how to do it properly.
Customizing WordPress using code snippets has a few risks. On the one hand, you may have no idea of what you’re copying. Sure, you know your expectations of copying that snippet, but you don’t know if it’ll actually do what you’ve been promised. And that’s normal! If you’re not a programmer or you don’t have any coding skills, you won’t be able to tell whether a snippet is safe and sound. Therefore, I’d recommend you copy and paste code only from trustworthy sites. For instance, you can trust ours. We wrote or at least tested all the snippets we share, so you shouldn’t have any trouble with them. I’d also recommend other sites like WP Mayor and WPTavern.
Another problem arises when the user has to paste that code somewhere. As far as I know, most websites will tell you to paste their snippets in
functions.php, a file all themes (including yours) have. In fact, we also recommended that file in the past. And it makes perfect sense to use that file for customizations—all sites will have it and, therefore, users won’t have any trouble finding and editing it.
However, saving your customizations in
functions.php has some undesired consequences. The most obvious are updates—when there’s a new version of your theme and you update it, you’re basically getting rid of all the old files and replacing them with the new version. Which means you just removed the
functions.php that contained all your customizations and replaced it with a brand new, customization-free file. Bummer!
Sure, you can fix this by having a backup ready. But that’s not exactly the solution; I’d say it’s a workaround if anything else. Backups are always a good idea and, in this case, they’ll make sure you never lose your customizations. However, it’ll be up to you to re-copy your customizations to your new functions.php file after every update… Trust me: remembering to do things manually is a recipe for disaster.
The solution is pretty obvious: if you don’t want a theme update to remove your custom code, just don’t add it in your theme. So, if you can’t add your customizations in
functions.php, where should you? Well, you simply need to create a tiny plugin that’ll hold your code! Don’t worry, it’s waaaaay easier than it sounds.
First of all, we’ll create a PHP file (let’s call it
nelio-customizations.php) with the required header:
This snippet is all we need to let WordPress know that this file is a plugin. Next, we need to add the custom code below this first comment. For example:
And that’s it! Now you simply need to upload it to
/wp-content/plugins/ and activate the plugin in your Dashboard (it’s the Customizations plugin):
If you want to make sure that your customizations will be always available and that none of your WordPress users will be ever able to deactivate it, don’t upload the plugin to
/wp-content/plugins/. Instead, upload it to
/wp-content/mu-plugins/ (starting with
mu-). As described in the Codex, plugins in this directory can’t be disabled from the Dashboard, execute before any other plugins, and their sole presence in
mu-plugins guarantees their being enabled.
Featured Image by Viktor Kern.