Skip to content
Gallery
100+ Handy WordPress Snippets For Improving Your Site's Functionality
Share
Explore

Must use plugin

A Must Use plugin is a type of WordPress plugin that is automatically activated when the site is loaded. They are stored in a special folder called "mu-plugins" and are not available for deactivation or deletion through the WordPress admin area.
They are typically used for site-wide functionality that should always be active, such as adding custom functionality or disabling certain features.

Basic Must Use Plugin

This is a basic starter template for creating a Must Use Plugin in WordPress.

This is a basic template for creating a Must Use plugin in WordPress.
Must Use plugins are automatically activated and cannot be deactivated by the user. They are typically used for site-wide functionality that is required on every installation of a site.
This template includes the basic header information that is required for a plugin, such as the plugin name, description, version, author, and license information.
It also includes a security measure to exit the script if accessed directly, to prevent any unexpected behavior.
This is just a starting point for creating a Must Use plugin and can be customized to add specific functionality to your site.

<?php
/*
Plugin Name: [PLUGIN NAME]
Description: [PLUGIN DESCRIPTION]
Version: 1.0.0
Author: [AUTHOR NAME]
Author URI: [AUTHOR URI]
License: GPL2
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Text Domain: [PLUGIN TEXT DOMAIN]
*/

// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
exit;
}


Here is an example of a basic Must Use plugin that adds a custom CSS class to the body of all pages on the site:
/*
* Plugin Name: Custom Body Class
*/

function custom_body_class($classes) {
$classes[] = 'custom-class';
return $classes;
}
add_filter('body_class', 'custom_body_class');
To use this plugin, you would save the code in a file called "custom-body-class.php" and place it in the "mu-plugins" folder in your WordPress installation.
The plugin would then automatically add the "custom-class" class to the body of all pages on your site, which you could then use in your CSS to style specific elements.

Create the directory

Please note that you need to create a folder named mu-plugins inside wp-content and put the plugin there.

image.png

Add index.php

To create this, you simply just go to your WP – content directory and create a new directory called mu-plugins, yeah also want to add a index.php file and inside that file to the opening PHP tag and the silence is golden comment just for some security purpose.
image.png

Preview in admin

We can now see when we go to the WordPress plug-in page we can see the Must-Use plugin menu option now, and when you click on open that you should see your active plug-ins with inside this directory.
image.png
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.