WordPress MUPlugins: What They Are + Useful MustUse Plugins For WP Web Developers

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!

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.

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.

A 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.

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');

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

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

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:

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.

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.

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

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:

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'] ) &amp;&amp; $_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…

WordPress Dashboard ScreenLook 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?

Beehive Pro Analytics screenOooh…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:

Incensy

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.

Keep reading the article at WPMU DEV Blog. The article was originally written by Martin Aranovitch on 2022-02-23 23:04:41.

The article was hand-picked and curated for you by the Editorial Team of WP Archives.

Disclosure: Some of the links in this post are "affiliate links." This means if you click on the link and purchase the product, We may receive an affiliate commission.

Leave a Comment

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

Show Your ❤️ Love! Like Us
Scroll to Top