Create a ‘Must Use’ Plugin for WordPress

Some of you are like me: “frequent theme changer guy”. Here’s a trick I’ve learned to keep from having to re-write code every time I get bored with a theme: I use the “Must Use” Plugin. 

A “Must Use” Plugin is a perfect solution for setting up Custom Post Types, Taxonomies, Google Analytics, and other important – but not necessarily “updatable” code that would otherwise be lost upon a theme change.

The general consensus among the WordPress elite is: CPT’s & Taxonomies should NOT be handled at the theme level, but rather, at the plugin level. A “Must Use” Plugin is code that cannot be deactivated or deleted unless you have FTP access.

Why not create a normal plugin or use functions.php?

You could. But there are extra steps to get it activated if you’re running a multi-site network. Furthermore, if others have access to the Plugins section of the WP Dashboard, there’s a chance it could be deleted or deactivated by mistake. Functions.php? You loose everything when change themes. By using a “Must Use” plugin, the code will always be available – no matter what network, theme, or curious guest blogger might do otherwise.

Ok, how do I create a “Must Use” Plugin?

  1. You need to create a directory: /wp-content/mu-plugins/
  2. Next, create a PHP file. You can name it anything you want. Mine is named grd_functions.php. The full path would look like: /wp-content/mu-plugins/grd_functions.php.
  3. Make sure you start the contents of the file with a <?php
  4. Now add your code, save your file and upload

That’s it! Really!

Check out the history of Must Use Plugins and learn more at the WordPress Codex

How about an example?

Here is grd_functions.php, which runs this web site:

https://gist.github.com/gregrickaby/6915797

Each and every time I change themes around here, I don’t have to re-create CPT’s, Taxonomies, Google Analytics, Custom Scripts, etc…Feel free to try my example, and never lose or have to re-write custom code again!

Image Credit: Armand9x

About Greg Rickaby

Director of Engineering @WebDevStudios / Author & Tech Editor @ForDummies / @WordPress Contributor / @gatsbyjs Maintainer

6 Comments

  1. Kevin Vella on August 6, 2014 at 12:34 pm

    I am a beginner using wordpress and I must say this article is a good starting point if you really want to get full control out of your wordpress site. A must read for all beginners. Thanks!

  2. Mike on June 20, 2016 at 11:55 pm

    Great stuff. Thank you so much.

  3. Sammy on January 31, 2017 at 10:56 am

    Nice code – this is a very neat way of handling an all too common problem… Thanks for sharing 🙂

  4. David Simmer on December 10, 2017 at 7:06 pm

    Minor typo (though you did employ it consistently): “maybe_override_contant” was probably intended to be “maybe_override_constant”?

  5. Jesse on June 22, 2018 at 5:22 am

    You can even “auto-load” normal WordPress plugins (folders) in the mu-plugins directory:

    https://github.com/littlebizzy/autoloader

    Original code from Roots, the forked version loads a bit faster. Cheers!

    • ThisHosting.Rocks on July 31, 2018 at 5:23 pm

      I’ll try out your autoloader. It’s exactly what I was looking for.

Leave a Comment