How To Add CSS Code In MU Plugin


  • Share on Pinterest

A must-use plugin (also called a ‘mu-plugin’) is a plugin that will always be activated by default, without you needing to do it yourself. To activate a mu-plugin, you just have to upload it in the right directory, and WordPress will automatically know that this plugin must be used. Moreover, a mu-plugin cannot be deactivated: it will always be executed, unless it is uninstalled.

Originally, mu-plugins were only available for WPMU, for those sites which used multiple (multisite) blogs. Back then, a mu-plugin was just a plugin activated for all blogs.

Fun fact, that the WordPress is loading mu-plugins in alphabetical order, before normal plugins.

All mu-plugins must be placed at wp-content/mu-plugins by default, in case /mu-plugins/ folder is not there, feel free to create it.

Add CSS code in mu-plugin

Create a file named style.php and add the following code inside.

<?php
add_action( 'wp_head', function() {
 ?>
 <style>
 
/* YOUR CSS CODE GOES HERE*/
 </style>
 <?php
} );

Add your CSS code between

<style></style>

tags.

 

 

Fell free to comment bellow if you have something new to add 🙂

  • Edurtu
    Reply
    Author
    Edurtu Edurtu

    Simple and effective … great!
    Thanks!

  • Eric Kowalewski
    Reply
    Author
    Eric Kowalewski Eric Kowalewski

    I tried this and can’t get it to work. Can you use hooks in the mu-plugins?

  • Beau Rotkowski
    Reply
    Author
    Beau Rotkowski Beau Rotkowski

    to do it? It wouldn t be any different to a regular plugin, except that MU plugins are a single PHP file only, so you d need to write the CSS in a PHP function, or store the CSS file somewhere else. Jacob Peattie Sep 18 at 8:58

Leave a Reply to Edurtu
Cancel Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Nastia