WordPress: Adding custom post types with a must-use plugin

Recently while working on a WordPress project, I decided to start a new theme from scratch. This, of course, meant taking a couple minutes porting some of my functionality from my old theme to my new theme. While it wasn’t back-breaking work, a lot of it can be avoided with some proper setup at the start of a project.

Custom post types were the best example of custom functionality I could think of that might disappear when changing themes. Lots of folks, when not using a plugin, declare their custom post types in functions.php, or somewhere similar. When you change themes, these inits are lost and some things will go wonky.

If you’ve got functionality you want to keep regardless of theme, consider throwing it inside a plugin, then throwing that plugin inside a /wp-content/mu-plugins/ folder. Files placed in the Must Use Plugins folder will automatically be used and never be able to be turned off. For things like custom post types or shortcodes, they’re perfect for making sure our functionality stays in place as we change themes.

Creating the plugin is dang easy. Simply create a new file and declare a new plugin like so:

Plugin Name: Must-use Loader
Plugin URI: http://clarklab.com
Description: A must-use plugin that initiates all our goodies.
Version: 1.0
Author: ClarkLab
Author URI: http://clarklab.com


// include the rest of your neatly sorted functionality here

Save the above file as something like must-use-loader.php and place it inside the /wp-content/mu-plugins/ folder. If this folder doesn't exist, create it. Once you've place the above file, your new functionality will immediately activate and will stay active until the file is removed. Nothing inside /wp-admin can change that. Only the file being physically removed will stop these functions.

In my sample above, once I've established the plugin, I simply include the PHP file containing my custom post type code, just like I used to in functions.php. The only difference is the next time I change themes I won't have to worry about any of this mess.