WordPress MU-Plugins: What They Are + Useful Must-Use Plugins For WP Web Developers

Do you need features to activate automatically and remain enabled on your website? Then consider adding these to WordPress as an mu-plugin…even create your own!

In this tutorial, we’ll explore:

What Are MU-Plugins?
A Brief History of MU-Plugins
MU-Plugin Features
How to Change the Default Must-Use Plugins Directory

When Not to Use MU-Plugins
How to Create and Install MU-Plugins in WordPress
MU-Plugin Tutorials
Hide WordPress Admin Item For All Users (Except You)
Hide WordPress Top Menu And Create A Custom Logout Button
Add CSS To the WordPress Backend
Remove All Styles From WordPress Content
Hide Elementor In WordPress Admin
Make Beehive Analytics Your WordPress Dashboard Home Page

Let’s jump right in…

What Are WordPress MU-Plugins?

Can mu-plugins make WordPress sites fly?

As described in WordPress.org’s documentation

Must-use plugins (a.k.a. mu-plugins) are plugins installed in a special directory inside the content folder and which are automatically enabled on all sites in the installation.

So, a must-use plugin (‘mu-plugin’) is just like any other WordPress plugin, except for the fact that when uploaded into the mu-plugins directory of the wp-content folder (i.e. /wp-content/mu-plugins it is automatically activated and remains enabled on your WordPress installation.

A Brief History of MU-Plugins

Back in the early days (around version 3.0), WordPress introduced a feature called Multisite (originally called WordPress MU) to allow multiple sites to share and be managed by one WordPress installation, WordPress Multisite (or WPMU as it became known) also introduced the ‘mu-plugins’ directory, offering site admins an easy way to activate plugins by default on all blogs in a multisite network.

Although MU-plugins originally referred to ‘multi-user plugins’, they were renamed to ‘must-use’ plugins.  Installed in the ‘mu-plugins’ directory of a WPMU installation, mu-plugins can now be used wherever you want one or more plugins to automatically activate and remain enabled, whether on one or multiple WordPress installations.

MU-Plugin Features

must-use plugin (‘mu-plugin’) is a plugin that, by default, is automatically activated and remains enabled when uploaded into the mu-plugins directory of the wp-content folder (i.e. /wp-content/mu-plugins. Once uploaded into this directory, WordPress will automatically know that this plugin must be used on the installation.

Must-use plugins are loaded before normal plugins and do not show in the default list of plugins on the Plugins page of wp-admin. They do, however, appear in a special Must-Use section in the Plugin screen’s list menu.

A Plugins screen with Must-Use plugins loaded on the site.

Notes:

A mu-plugin cannot be deactivated or disabled except by removing the plugin file from the must-use directory of your WordPress installation.
While the plugin remains in the directory, WordPress will always execute it. So, once an mu-plugin has been added to the mu-plugins directory, it remains “always on” until it is removed from that folder.
Must-Use plugins do not update automatically and WordPress doesn’t notify you about new versions. This means that mu-plugins have to be manually kept up-to-date (unless installed and maintained by an external service).
Must-Use plugins can be enabled by simply uploading the file to the mu-plugins directory, without having to log-in.
MU-plugins are loaded by PHP, in alphabetical order, before normal plugins. This means that API hooks added in an mu-plugin will apply to all other plugins even if they run hooked-functions in the global namespace.
Also, keep in mind that mu-plugins will work on both single and multisite WordPress installations but will apply to every site in a multisite network.

How to Change the Default Must-Use Plugins Directory

To change the default directory manually, you will need to define two constants (WPMU_PLUGIN_DIR and WPMU_PLUGIN_URL) in wp-config.php:

define(‘WPMU_PLUGIN_DIR’, ‘/full/path/to/new/directory’);
define(‘WPMU_PLUGIN_URL’, ‘https://URL/to/new/directory’);

Notes:

If you define the above constants and the path is not valid, WordPress won’t redefine the constants and your mu-plugins won’t load.  So, unless you have a reason to change the default mu-plugins directory, we recommend sticking with the default values for WPMU_PLUGIN_DIR and WPMU_PLUGIN_URL.
If changing the default directory, these two constants must be defined in the wp-config.php file before the line where the wp-settings.php file is included, so you won’t be able to use useful constants (e.g. WP_CONTENT_DIR).

When Should You NOT Use MU-Plugins?

Must-use plugins let you add features to the WordPress core that are not installed by default but that users may need.

However, as stated in the WordPress documentation

Despite its suitability for many special cases, the mu-plugins system is not always ideal and has several downsides that make it inappropriate in certain circumstances.

The documentation then goes on to list a number of important caveats to keep in mind:

Plugins in the must-use directory will not appear in the update notifications nor show their update status on the plugins page, so you are responsible for learning about and performing updates on your own.
Activation hooks are not executed in plugins added to the must-use plugins folder. These hooks are used by many plugins to run installation code that sets up the plugin initially and/or uninstall code that cleans up when the plugin is deleted. Plugins depending on these hooks may not function in the mu-plugins folder, and as such all plugins should be carefully tested specifically in the mu-plugins directory before being deployed to a live site.

Note: WordPress only looks for PHP files right inside the mu-plugins directory, and (unlike for normal plugins) not for files in subdirectories. You may want to create a proxy PHP loader file inside the mu-plugins directory:

<?php // mu-plugins/load.php
require WPMU_PLUGIN_DIR.’/my-plugin/my-plugin.php’;

Keep It Simple With Mu-Plugins

Mu-plugins should only be used to do “one” simple action. Calling complex code all the time might lead to conflict with other code or plugins and involving mu-plugins as suspected sources when troubleshooting conflicts can really complicate things.

MU-Plugins Can’t Be Stored in Subdirectories

Additionally, keep in mind that, unlike regular plugins, you can’t store an mu-plugin in a subdirectory of the mu-plugins directory. This means that you won’t be able to sort your mu-plugins using folders by default.

You could get around this by creating an mu-plugin to make WordPress automatically included mu-plugins stored in subdirectories, but as they are not found by WordPress by default, they won’t appear in the must-use plugins list, so you won’t be able to see which must-use plugin is installed in subdirectories without looking at the mu-plugins directory itself.

How To Create and Install MU-Plugins in WordPress

Adding a must-use plugin to your site requires three steps:

Create an mu-plugins directory (folder) in the wp-content directory
Create an mu-plugin php file
Upload/install the file on your site via FTP

We have created an excellent step-by-step tutorial that shows you how to install MU-plugins in WordPress in our documentation section.

Follow the above tutorial to go through the entire process, or click on a link below to go to one of the above steps:

Create an mu-plugin directory
Create an mu-plugin
Upload/Install an mu-plugin file

Add These Useful Must-Use Plugins To Your WordPress Toolbox

Warning: We strongly recommend performing a complete backup of your site before changing things or adding code to server files in case anything goes wrong. Snapshot makes backing up and restoring WordPress sites a breeze! 

If you are a WordPress developer or manage websites for your team or your clients, here are some useful mu-plugins you can create to expand your bag of tricks:

1. Hide WordPress Admin Bar For All Users (Except You)

If you manage websites for your team or your clients, or are the head developer of your business, you may want to make WordPress customizations that only you can see, or hide buttons for all users other than you to be sure people don’t click where they shouldn’t .

While you can customize admin tools using a plugin like Branda, you can also do this easily using code.

For this example, let’s say that you are the only superadmin of a site and want to hide the toolbar for every user except yourself.

Just create a mu-plugin and add the following code and style it using CSS:

<?php
add_action( ‘admin_footer’, ‘hideitems’ );
function hideitems() {
if ( ! is_super_admin() ) {
echo «<style>
#wpadminbar {
display: none !important;
}
</style> «;
}
}

2. Hide WordPress Top Menu And Create A Custom Logout Button

Here’s another WordPress admin customization you can do for your clients.

Let’s say you want to hide or remove the top bar on your client sites’ admin areas because you find it’s inconvenient to have it display there or feel that giving clients who don’t know much about WordPress access to content modification menus could lead to them making inadvertent mistakes.

For example, if you build sites using Elementor, your clients can access Elementor’s theme customization options directly from the top menu bar.

Users can add a new Elementor template in their WordPress toolbar.

Sticking with Elementor for this example, you may decide to hide this to avoid problems and leave the theme customization path inside the admin area at Templates > Theme Builder. Or you may decide to remove it altogether, because even if you disable options from the admin bar, some users may still not understand what it is and why it displays on the front end of their sites.

Hiding the top bar is very easy to do using a plugin like Branda…just go to Admin area > Admin bar and then uncheck the toolbar visibility for some users.

The problem is, if you completely disable this bar, users won’t be able to logout from their admin. This not only creates a security issue, it’s also not convenient if you want to switch users.

Fortunately, there is a solution: Simply add this code as a mu-plugin to create a logout button:

<?php
add_action(
‘admin_menu’,
function () {
add_menu_page(
‘Logout’,
‘Logout’,
‘read’,
‘wp_custom_logout_menu’,
‘__return_false’,
‘dashicons-marker’,
1 // Here use 1 for placing menu on top or PHP_MAX_INT to place it at the bottom,
);
}
);

add_action(
‘admin_init’,
function () {
if ( isset( $_GET[‘page’] ) && $_GET[‘page’] == ‘wp_custom_logout_menu’ ) {
wp_redirect( wp_logout_url() );
exit();
}
}
);

This will redirect your user to the login page after the logout process.

3. Add CSS To WordPress Backend

How can you add CSS to the WordPress backend?

Once again, you can use a plugin like Branda, but you could also create an mu-plugin and paste the following code in your file:

<?php
add_action( ‘admin_footer’, ‘my_admin_css’ );
function my_admin_css() {
echo ‘<style>
/*Put your CSS code here*/
</style>’;
}

That’s it…easy and very handy!

4. Remove All Styles From WordPress Content

Suppose you come across a site where all the content style is set by HTML within the WordPress content.

This is kind of a bummer, since no one really wants to modify the style of each page when you can easily do this using CSS.

So, here’s a workaround for this.

Create a mu-plugin, and add the following code:

add_filter(
‘the_content’,
function ( $content ) {
// Delete all inline style.
return preg_replace( ‘/ style=(«|’)(.*?)(«|’)/’, », $content );
}
);

Try it out!

5. Hide Elementor In WordPress Admin

Elementor is one of the popular and widely-used page builders for WordPress. If you are a web developer or webmaster, you may be using Elementor to create and manage a website and its content.

As easy as Elementor is, it still requires some knowledge to use it and an inadvertent mistake by a user could make the site impossible to navigate.

So, if you are the only one managing the website and users don’t need to create pages in WordPress very often, you might want to hide Elementor options from other backend users. Making sure there is no modification happening to the backend without your knowledge will give you peace of mind and your customers or backend users may also appreciate having a simpler interface to work with.

This tip works well on the following kinds of sites:

eCommerce sites
Websites with custom posts types
Websites used mainly as a blog to publish articles.

As stated earlier, if end users need to regularly create WordPress pages, this might not work for you because they will need Elementor for this.

Before creating an mu-plugin, first you must hide Elementor in the menu. You can then hide the Elementor button on WordPress pages.

1. Hide Elementor in the menu

Install Branda and then:

Go to Admin area > Admin bar and hide it for your end users. This way they will not be able to access the Elementor builder from there.
Go to Dashboard > Admin menu > Customize and then hide the Elementor admin menu for everyone except you (tip: use user roles for this as it is easier than doing it for every user).
Remember to also hide the “Templates” menu (this is probably the most critical thing to hide).

Note: We recommend that only one person should be able to access the templates, especially if using these to build your theme and using CSS to control every item of your site’s design. This prevents other users from adding CSS elsewhere (e.g. in builders, theme customizer, stylesheets, etc.)…and without commenting properly when adding code, e.g.

/* Header customization */
.header{
margin-top: 10px;
}
/* END Header customization */

2. Hide the Elementor button on WordPress pages

The easiest way to create or modify a page with Elementor is simply to go to Pages > Create and click on the Elementor button to change it.

There is also a way to hide this button if you do not want users to access Elementor at all.

To do this, you need to customize user roles. If you are the only admin of the site, you can hide the buttons for everyone except you.

Note: You can use the previous step to hide the Elementor templates only (to keep your theme safe) but still leave access in Elementor to create and modify pages. This, however, can be risky, since when users are on an Elementor page, they can navigate to other elements like the header or footer, or access Elementor global settings that will affect the site.

Once you have done the above, the only thing left to do is create a mu-plugin and paste the following code to hide the button :

<?php
add_action( ‘admin_footer’, ‘hideitems’ );

function hideitems() {
if ( ! is_super_admin() ) {
?>
<style>
#elementor-switch-mode {
display: none;
}
</style>
<?php
}
}

Enjoy!

6. Make Beehive Analytics Your WordPress Dashboard Home Page

If you are not a huge fan of the WordPress admin page (even if you can customize its widgets and hide panels) or having your users see this screen every time they log into their WordPress dashboard…

Look but don’t look…is this what you want users to see when they log in?

Why not show them a beautiful admin landing page with useful information like this?

Oooh…aaah…charts and stats…that’s much better!

Again, you can use Branda to customize the admin panel and the left menu or hide/show menus depending on the user role. This is great if you are the only admin and want to display the minimum required menus for end users or add custom CSS.

If, however, all you want to do is redirect users from the WordPress admin page to a more useful page like the Beehive analytics page, then simply create an mu-plugin and add this code:

<?php
function wpmudev_redirect_page() {
global $pagenow;
if ( $pagenow == ‘index.php’ && ! wp_doing_ajax() && is_plugin_active( ‘google-analytics-async/google-analytics-async.php’ ) ) {

wp_redirect( admin_url( ‘/admin.php?page=beehive-google-analytics’ ) );
exit;
}
}

add_action( ‘admin_init’, ‘wpmudev_redirect_page’ );

Enjoy!

Make WordPress Fly With MU-Plugins

Hopefully, now you know all about Must-Use plugins: what they are, when and when not to use them, and how and where to install them on WordPress sites.

Check out our mu-plugins documentation, try creating and installing some must-have plugins of your own, and if you experience any issues or need expert help, contact our support team…we’re available 24/7 to answer any WordPress questions you have!

Contributors

This article was written in collaboration with:

Antoine –  Incensy.  I am passionate by web culture and digital solutions. I have been working in this area for 11 years now. Always looking for the best solutions to help my customers overcome digital challenges and grow their business online. My vision of digital success: Get a global strategy, apply it with detailed precision, and always keep improving.

***

Note: We do not accept articles from external sources. WPMU DEV members, however, may contribute ideas and suggestions for tutorials and articles on our blog via the Blog XChange.

Щелкните ниже, чтобы поставить оценку!
Всего: 0 В среднем: 0

Написать комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *