I’ve been experimenting with creating a Custom Post Type as a plugin (because I’ve seen this recommended in various places).
But I have a question not about the how, but rather the why of building CPT’s as a plugin.
Yes… Adding the CPT as a plugin does keep my functions.php nice and tidy.
But…
- Assuming I’ve enabled
has_archiveI still need to createarchive-cpt.php, right? - Also: In order to display the CPT I need to create a custom loop, so I still need to create
single-cpt.php…right? - And these files need to be created in the theme, right?
If I understand this correctly,
- If I disable the plugin: I still have to remove (or hide or something) the
archive-cpt.phpandsingle-cpt.phppages. - If I switch Themes: I still have to add those two pages into the new theme. Right?
I haven’t even touched the issue of adding a CPT to the default loop (and it’s implications on plugin-based cpt’s).
So, why a CPT plugin?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
toscho’s answer is correct as to the technical reasons to define your CPT in a plugin, but it seems to me that much of you question stems from a misunderstanding of the template hierarchy. Almost, but not quite, every template file that you’ve seen is optional.
With the exception of the basic index.php template file, Theme
developers can choose whether they want to implement a particular
template file or not. If WordPress cannot find a template file with a
matching name, it skips down to the next file name in the hierarchy.
If WordPress cannot find any matching template file, index.php (the
Theme’s home page template file) will be used.
WordPress will use the particular specialized files if they exist but fall-back to another file– ultimately index.php— if there are no specialized files. Your theme does not have to implement anything special to deal with or compensate for your plugin’s CPT’s. The theme can but does not have to.
- Assuming I’ve enabled
has_archiveI still need to createarchive-cpt.php, right?
No. archive.php will be used and if that fails then index.php
- Also: In order to display the CPT I need to create a custom loop, so I still need to create
single-cpt.php…right?
Again, no. Same reason. single.php will be used and if not index.php.
- And these files need to be created in the theme, right?
Yes, but they are optional. You don’t need them at all.
- If I disable the plugin: I still have to remove (or hide or something) the
archive-cpt.phpandsingle-cpt.phppages.
No. You don’t need to do anything. The templates will not be used.
- If I switch Themes: I still have to add those two pages into the new theme. Right?
Wrong. The templates are optional. You only need them if you want a customized display for the post type.
When you understand that the theme and the CPT are not as intimately connected as your question make it seem, then some of the other logic should make a bit more sense.
Method 2
The theme isn’t loaded when the constant SHORTINIT is set to TRUE (custom AJAX handlers, importers or APIs), no posts can be added to such a custom post type or taxonomy then.
The templates are the views for the custom content, they should not define or rely on the logic.
Also, after a theme switch, the user cannot access and change the post type content anymore, because there would be no interface without the registration.
After disabling a plugin you don’t have to change the theme. The templates are just not used anymore.
Update: Another advantage of plugins is the ability to activate them network wide. I am the main developer for Multilingual Press, and we offer our users a feature to translate and connect custom post type posts. But this cannot work if they are bound to a theme, because a theme is always active per site, not across the whole network. Theme post types are really hard to translate.
See also: Where to put my code: plugin or functions.php?
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0