0

I'm going through a tutorial which says that creating new post_types is best handled by creating a new mu-plugin (as opposed to adding it to the functions.php). I understand why, but what I don't understand is how to include this mu-plugin as a dependency of my theme.

I want to be able to have my friend upload my theme and have it work right away, without having to tell them to copy the mu-plugin file into their WordPress mu-plugins folder.

3
  • 1
    I strongly disagree with this tutorials recommendation, post types should be in a normal plugin. I would be very suspicious of other things this tutorial says – Tom J Nowell Apr 30 '20 at 17:19
  • Hmmm ok. Good to know :/ – exxodus7 Apr 30 '20 at 19:58
  • there's a lot of tutorials out there, lots of them do things the old way, or they recommend questionable practices, instead defer to what the .org handbooks say on developer.wordpress.org, and put CPT's in plugins. Keep themes to visuals only. You'll see a lot of premium themes do pretty terrible things, and popular plugins doing not so great things because they're stuck doing it that way – Tom J Nowell May 1 '20 at 10:21
1

The reason you are advised to put a custom post type in a plugin rather than your theme is that in this way the user can keep the cpt even if he decides to switch to another theme. In five years or so, design trends may change significantly and you don't want to be stuck with a theme, just because it also holds your custom post definition.

Now, if you look at the hook order, you see that mu-plugins are loaded before everything else. This means that the main function of the plugin is registered by the time you initialize your theme at the after_setup_theme hook. At that point you can use function_exists to check if the plugin has been loaded. If not your can notify the user or do something more drastic, like stop loading the theme.

By the way, it is not necessary to make your plugin a must use. The plugins_loaded hook is also executed before after_setup_theme, so any plugin function is known to WP before you start initializing the theme.

Also, it is possible to make the dependency a two way affair. If your plugin initializes (as it should do) at the init hook, the theme has been loaded by that time, so you can check for the existence of the theme's main function and issue a warning to the user that he is not using the theme that is optimal for this custom post type.

3
  • 1
    Thank you for your answer! "At that point you can use function_exists to check if the plugin has been loaded. If not your can notify the user or do something more drastic, like stop loading the theme." Can I have the theme install the plugin if it doesn't find it? I'd like to not just issue a warning to the user that their theme is missing some required software, but install it. How do themes I download do this? Do they just include the post_types in their functions.php file? Are mu_plugins just for users who are actively maintaining their own sites? – exxodus7 Apr 30 '20 at 16:53
  • I'm with Tom and cjbj on this... ...there's no need for it to be an mu-plugin. Additionally, you don't have to show an 'error' or an alert. I recently put together a CPT plugin, and the theme uses the CPTs on the front page, but what if the plugin fails or is corrupted or whatever... ...so what I did was integrate the two as dependencies with graceful fallbacks. Instead of 'notifications' what I do is make the integration with the theme dependent on whether or not the CPT plugin is active, but if it isn't, I make sure the theme can run perfectly without. I can post examples if you need. – Tony Djukic May 1 '20 at 3:11
  • Acutally installing a plugin from a theme would be very hacky indeed. If you insist on shipping the cpt functionality with your theme, you could isolate it in a separate php file. Any future theme developer could then easily turn it in a plugin or include it in his own theme. – cjbj May 1 '20 at 7:33
0

Don't

Just register your post types in a normal plugin in the plugin folder. You don't need an mu-plugin to use CPT's.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.