I’m wondering what is the best place to put the php code for defining custom post types.
Looks like theme functions.php is a big no-no because CPT are lost when switching theme
Wordpress codex recommends defining CPTs inside a plugin code
Some authors of tutorials I read recommend to define CPTs in a mu-plugin, which cannot be accidentally deactivated. That sounds like a great idea to me
Anyway, is there a wordpress (or Wordpress developer community) best practice?
Many thanks
I’m wondering what is the best place to put the php code for defining custom post types.
Looks like theme functions.php is a big no-no because CPT are lost when switching theme
Wordpress codex recommends defining CPTs inside a plugin code
Some authors of tutorials I read recommend to define CPTs in a mu-plugin, which cannot be accidentally deactivated. That sounds like a great idea to me
Anyway, is there a wordpress (or Wordpress developer community) best practice?
Many thanks
Share Improve this question edited Sep 22, 2019 at 8:07 fuxia♦ 107k39 gold badges255 silver badges459 bronze badges asked Sep 22, 2019 at 7:41 Guido BraslettiGuido Brasletti 11 Answer
Reset to default -1I think that this choice really depends on the project. If a post type is intrinsically linked to the theme. Maybe your theme will have a js and css bundle that includes post type features. In this case it is better to define it immediately in this one. But it really depends on the project and how the code is organized. There is no "THE right method".
But if the custom post type brings new features that may potentially need to be deleted (for example: an event list on a site). It is better to define them in a plugin or mu-plugin.