最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to include a mu-plugin as a theme dependency?

programmeradmin3浏览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.

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.

Share Improve this question edited Apr 30, 2020 at 16:21 cjbj 15k16 gold badges42 silver badges89 bronze badges asked Apr 30, 2020 at 16:14 exxodus7exxodus7 1112 bronze badges 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 Commented Apr 30, 2020 at 17:19
  • Hmmm ok. Good to know :/ – exxodus7 Commented Apr 30, 2020 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 handbooks say on developer.wordpress, 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 Commented May 1, 2020 at 10:21
Add a comment  | 

2 Answers 2

Reset to default 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.

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.

发布评论

评论列表(0)

  1. 暂无评论