A must-use plugin (also known as ‘MU-Plugins’) is a WordPress plugin that resides in a special folder
mu-plugins in a content directory
wp-content and will always be activated by default (always active) for a website and network websites. These plugins are not visible among the usual plugins in the admin panel – they are displayed in the top information bar and can not be just disabled, otherwise than by removing the plugin file in the directory
Also, these plugins cannot be placed in folders – the file shoulв be placed in the folder
wp-content/mu-plugins. I.e., WordPress automatically connects all the files from the folder mu-plugins, but does not check the subfolders, which can also contain files. Connection of files from subfolders should be prescribed i manually n a file from the main folder.
Directory of plugins can be changed, therefore, you need to define constants:
WPMU_PLUGIN_URL in the file
Advantages of Must-Use Plugins
- They’re always on. There’s no need to activate it in the admin panel. Users can not disable them, intentionally or accidentally;
- They can be connected and activated by adding a file of a plugin in directory wp-content/mu-plugins, without the need of authorization on the website;
- PHP files of the directory are loaded in alphabetical order, before the usual plugins have been loaded. This means that we can verify in advance, set hooks in advance, if necessary.
Disadvantages for must-use plugins
In most cases there is no need to use these plugins, because conventional plugins are more convenient. Let’s consider disadvantages of the MU plugins:
- They are not checked for the presence of updates, so if the new series of plugin is active, we won’t see a notice to update the plugin. So, you need to track a new version;
- Hooks of activation / deactivation of the plugin do not work, no matter that they are responsible for events related to the plugin installation or removal. Therefore, in course of activation you will have to add a table or an option in a database and do other things, and in course of deactivation delete everything associated with the plugin from the database and files, by yourself.
- WordPress is looking for php files in the my-plugin directory and does it not in the same manner like for usual plugins – does not view the files inside the subfolders. In this case, you will need to create a boot file in the my-plugin directory, so that is would connect files from the subdirectories, as indicated below:
// mu-plugins/load.php require WPMU_PLUGIN_DIR.'/my-plugin/my-plugin.php';
The question now arises: “In which cases can be useful to use the mu plugins?”. An interesting question and the answer is: “In cases where it is more convenient than a conventional plugin …” For example, I have recently put this code in the form of this plugin to install 301 redirects from the old URL, when I changed the NC on a long time working website. It seemed to me the best solution because:
- It is wrong to insert such redirect to the theme – the theme can be changed suddenly and all redirects will disappear, and there are back-links …;
- It can be installed as a plugin, but if you turn it off accidentally, you will loose all redirects, and it can be unnoticed.
Article last updated on November 22, 2020