We’ve all been there – you have a lovely wordpress site you’ve been maintaining and building upon for ages, as your client asks for more and more customisations you find yourself adding more and more code to your functions.php file to acheive them.
It’s not long before your functions.php file gets very long and begins to be a pain in the *** to read. You may find yourself wondering “what does that do again?” and when it comes to redeveloping a new theme for the site you find you have a lot of code to copy into your lovely, clean functions.php. Not cool. Thankfully this is where WordPress’s must-use plugins (AKA mu-plugins) come in handy. You can save each bit of code into seperate, meaningful files and have them automatically loaded into your site.
All you need to do is create a new folder called ‘mu-plugins’ right next to the plugins folder. i.e. wp-content/mu-plugins
Once you’ve done that add the following php to a new file inside the directory and save it with whatever file name suits you – e.g. customisation.php
<?php /** * Plugin Name: * Description: * Author: */ // begin custom code // end custom code ?>
Give your mu-plugin a name & description so you easily remember what it does and put your name on it incase anyone else works on the site and needs to know who made it. Put your custom code in the labelled area and upload the file.
Simple hey? When you next log into the admin area of your site you will notice a new ‘mu-plugins’ section in the plugins area.
Things to keep in mind.
The mu-plugins have many advantages and a couple of potential disadvantages. Try and keep these points in mind:
- They’re always on. Basically you don’t have to enable them and you cannot disable them.
- They’re loaded before normal plugins and in alphabetical order.
- You can’t use activation/deactivation hooks (because they can’t be activated or deactivated).
- You have to use PHP files not inside folders. WordPress only looks for PHP files directly in the mu-plugins folder, not in sub-folders.
For a full and detailed explanation of must-use plugins and all the possibilities the present check out the entry in the WordPress Codex