Search

WordPress Must-Use Plugins: A Breakdown

Listen to this article
did-you-know
Pic Courtesy: GoogleFaxOnline

I’m new to WordPress. Started my WordPress journey about 3 months ago at WisdmLabs. Every day since has been of new experiences, new learnings and new findings.

Here’s my attempt at documenting one of these..

It’s my debut post and I’m going to talk about WordPress Must Use plugins. Also popularly known as MU-Plugins. Already know about these? There’s always a chance you might not know enough.

Let’s dive deep into WordPress Must Use Plugins!

[space]

[space]

[space]

What is a WordPress Must Use Plugin?

A WordPress Must Use Plugin is a plugin that is activated by default on your WordPress site. Originally used by multisite admins to activate a plugin by default on all the sites on their network, it was later integrated into core WordPress. This move allowed users to take advantage of WordPress Must Use plugins without a multisite installation.

Elaborated below are the defining features of a WordPress Must Use plugin and how it differs from a traditional plugin.

  • Activation: By definition, WordPress Must Use plugins are activated by default. You do not need to explicitly activate them.
  • Storage: WordPress Must Use plugins are stored in the ‘mu-plugin’ directory of the  ‘wp-content’ folder whereas conventional plugins can be found in the ‘plugins’ directory of the same.
  • Deactivation – Unlike the traditional plugins, WordPress Must Use plugins cannot be deactivated from the dashboard. Their execution can only be prevented by deleting them from the ‘mu-plugin’ directory, for which you will need FTP access.
  • Loading Priority: WordPress Must Use plugins are loaded before normal plugins.
  • Usage of Hooks: You can’t use activation or deactivation hooks for WordPress Must Use plugins. A task that needs activation hooks cannot be included in a Must Use plugin.
  • Updates: WordPress Must Use plugins don’t update automatically, and WordPress doesn’t notify you about new versions. This means that you have to keep them up-to-date manually.

[space]

Creating a Must-Use Plugin

  1. Navigate to ‘/wp-content/’ and create a directory called as `mu-plugin`. This is the default directory for WordPress to store mu-plugins.
  2. If you want to change this default directory, open the file listed as wp-config.php and add following declarations to it:
define('WPMU_PLUGIN_DIR','absolute_path_of_mu-plugin_folder');
define('WPMU_PLUGIN_URL','http://absolute_url_of_mu-plugin_folder');

Define this constant before the file includes wp-setting.php. Any mistake in the path will lead to WordPress failing to find the plugin.

  1.   Coming back to your new directory, create a new php file with name `my-first-mu-plugin.php`
  2.   Open the file and add the following codes:
<?php
/*plugin name: Test mu-plugin
Description:This is just a test plugin
Author:WisdmLabs
*/
function Test(){
    echo '<p style="float:left; padding: 10px;margin:3px; background-color: gray;">WisdmLabs</p>';
}
add_action('wp_footer','Test');
?>

That’s it. You have now created your first mu-plugin. Goin back to your site,  you will now be able to see the following on the bottom left corner of the website:

must-use-plugin-example

If you have multiple Must-Use plugins running on your website, you’ll want to see a complete list of those plugins. Navigating to the Plugins tab under your Dashboard, you should now see an option that reads ‘Must-Use’ listed along with ‘All’ and ‘Inactive’ as shown below.

wordpress-must-use-plugins

Here’s how a sample ‘mu-plugin’ directory with 4 mu-plugins would look like,

 muplugin-directory

Sub Directories MU-Plugins

All PHP files present in your ‘mu-plugin’ directory will be listed as a plugin on your site, but directories present inside will NOT be read. All the PHP files within a subdirectory will not be executed by WordPress as plugins.

This is not to say you cannot keep your plugins separated by folders. To do this, you will need to create a PHP file wherein you will type in a code to include PHP files of all the other plugins. Let’s assume the file is called ‘function.php’. Inside, you will write:

require(WPMU_PLUGIN_DIR . '/mu-plugin1/mu-plugin1.php');
require(WPMU_PLUGIN_DIR . '/mu-plugin2/mu-plugin1.php');
require(WPMU_PLUGIN_DIR . '/mu-plugin3/mu-plugin1.php');
require(WPMU_PLUGIN_DIR . '/mu-plugin4/mu-plugin1.php');

Where your folder structure will be like:

muplugin-subdirectory

Instead of writing a code to include individual files in this manner, you can add this, which will let you include all files:

//open mu-plugin directory  
$wdm_mu_plugin_dir = opendir(WPMU_PLUGIN_DIR);  

//to get files in directory
while(($file=readdir($wdm_mu_plugin_dir))!==false) {
   $file= WPMU_PLUGIN_DIR . '/' . $file;

   //if file is directory
   if($file!='.' && $file != '..' && is_dir($file)) {
     //requires plugin
     require($file . '/' . $file .'.php');
   }
}
//to close directory
closedir($wdm_mu_plugin_dir);

There. You can now conveniently arrange your plugins in multiple directories.

[space]

Where can I use Must-Use Plugins?

Now that you have created a Must-Use plugin, let’s take a look at the way in which you can use it.

  1. Multisite network: Mu-plugins are especially handy when operating on a multisite network. As an admin, you can set a plugin as mu-plugin if you want it to apply it to all your client sites by default. People using your client sites will not be able to deactivate those plugins.
  2. To execute a code by default no matter what: One of the cases where this might be particularly useful is when a piece of code is absolutely essential to a client website, without which the site would malfunction. Using a “Must Use” plugin, the code will always be available, regardless of the network.
  3. Site-Specific Plugins: If you often add snippets of code to extend the functionality of your website, creating a site-specific plugin is a way better option as compared to editing functions.php of your theme. So that even when you switch themes, you won’t have to manually add the snippets all over again to regain the lost functionality, you can just re-activate the site-specific plugin instead.But site specific plugins are still prone to activation and de-activation, which may not be desirable by the site administrator. To circumvent this, set the site- specific plugin as a WordPress Must Use plugin to ensure it’s always activated.

So this was all about Must-Use plugins and their usefulness while developing a website. Here’s hoping you have a good time with them. Stay sharp!

Prachi Vidhate

Prachi Vidhate

One Response

Leave a Reply

Your email address will not be published. Required fields are marked *

Get The Latest Updates

Subscribe to our Newsletter

A key to unlock the world of open-source. We promise not to spam your inbox.

Suggested Reads

How to Make Responsive Tables using CSS without Tag
Join our 55,000+ Subscribers

    The Wisdm Digest delivers all the latest news, and resources from the world of open-source businesses to your inbox.

    Suggested Reads