I’m writing a custom post type plugin. Part of it I’m outputting to the template via shortcodes. But other parts need a custom post template, and I figured out how to use the template hierarchy for CPT’s. But the custom template is in the theme, and I’m thinking the plugin should be self-contained, at least to start with.
So what’s the best practice here? How do we include template files in a CPT plugin? Can you point me to especially good examples of how this is done?
Thanks for your help.
I would say a combination of letting the theme handle it and providing a default with your plugin.
You can use the
single_template
filter to switch out the template. In your callback, see if the theme provided a template for the post type, if it did, do nothing.I like this method the best. Combine it with providing a sound set of “template tags” (eg.
the_content
,the_title
) that support whatever custom data that goes along with your post type and you give the end user a lot of customization power along with some sound defaults. Bbpress does this sort of thing really well: includes user templates if it finds them and provide a lot of template tags.Alternatively, you can use a callback with
the_content
filter, and just change stuff in the content itself.You could hook into
template_include
and return your plugin file if the request is for your post type:But this will change the look drastically. There is still no clean solution.