Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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...

  1. Assuming I've enabled has_archive I still need to create archive-cpt.php, right?
  2. Also: In order to display the CPT I need to create a custom loop, so I still need to create single-cpt.php ...right?
  3. And these files need to be created in the theme, right?

If I understand this correctly,

  1. If I disable the plugin: I still have to remove (or hide or something) the archive-cpt.php and single-cpt.php pages.
  2. 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?

share|improve this question
    
Of course, after writing this question I saw this post. But I'm still not sure. Is the selected answer considered a "Best Practice"? Please advise. –  sleeper May 11 '13 at 21:51
1  
you don't need templates specific to single and archive cpt views, only if you want them presented differently than the single and archive templates your theme falls back to, which may just be index.php in some cases. –  Milo May 11 '13 at 22:04
add comment

2 Answers 2

up vote 4 down vote accepted

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.

http://codex.wordpress.org/Template_Hierarchy

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.

  1. Assuming I've enabled has_archive I still need to create archive-cpt.php, right?

No. archive.php will be used and if that fails then index.php

  1. 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.

  1. And these files need to be created in the theme, right?

Yes, but they are optional. You don't need them at all.

  1. If I disable the plugin: I still have to remove (or hide or something) the archive-cpt.php and single-cpt.php pages.

No. You don't need to do anything. The templates will not be used.

  1. 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.

share|improve this answer
    
OK. Now I get it. –  sleeper May 11 '13 at 23:58
add comment

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?

share|improve this answer
    
"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." ??? I have no idea what that means (yet). I'm just getting into CPT's. Please explain. –  sleeper May 11 '13 at 21:59
    
"Where to put my code: plugin or functions.php?" - Excellent discussion. Seems I've stumbled into a Hornet's Nest. –  sleeper May 11 '13 at 22:04
    
Ok, so no "simple" answer to this question, eh? The link seems to provide the best info on how to "decide" where to put the CPT (based on several variables). –  sleeper May 11 '13 at 22:11
    
The simple answer is: Custom post types work better in plugins. –  toscho May 11 '13 at 22:12
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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