I would like to get some opinions regarding the best practices for developing WordPress plugins that provide theme integration.
In order to make sense as I ask this question, let me start with an hypothetical example of a scenario that I am curious about. Imagine that I create a plugin called “Discography”. Discography registers three custom post types: “Bands”, “Albums”, and “Tracks”. The plugin also provides meta boxes that provide details for each post type, as well as, custom taxonomies to organize each post type. These post types are tied together with the Posts 2 Posts plugin. Within the admin, the user can add new bands, which can be associated with albums, which, in turn are associated with tracks, all which will have lots of other data added to them via meta boxes and taxonomies.
Now, I don’t want this plugin to simply set up an admin for users to enter this information; I would want it to provide some default displays for the data. A more advanced user/developer would be fine having only this admin. It would be easy enough for her to grab that data and use in the theme; however, without some default views, this plugin would be useless for most users. For this example, you could display anything like (parentheses show the ways the info could be displayed in order of the template hierarchy):
- Bands (single-prefix-band.php, single.php, index.php, shortcode)
- Albums (single-prefix-album.php, single.php, index.php, shortcode)
- Tracks (single-prefix-track.php, single.php, index.php, shortcode)
- Band Listing (template-band-list.php, page-band-listing.php, page-{id}.php, page.php, index.php, shortcode)
- Album Listing (template-album-list.php, page-album-listing.php,
page-{id}.php, page.php, index.php, shortcode) - Album Timeline (template-album-timeline.php, page-album-timeline.php,
page-{id}.php, page.php, index.php, shortcode)
It’s important that there is some default presentation for these post types as the default template files would not display all of the information that is needed for each of the post types. For instance, the Twenty Eleven theme, by default, would just show the name, categories, description and post date for an album. Not very useful for an album. I would want to provide a single post template that pulls in the band, release date, record label, album versions, tracks, etc. As a plugin developer I would feel that that would be important to provide. I know the template wouldn’t work out for every theme, but there should be some default that can be further integrated with the user’s theme.
Again, I’m curious about what is the best way to handle this situation? I think you could do any of the following.
Shortcodes
Shortcodes could be used as a very flexible and user friendly way to allow non-devs to add a bands, albums, tracks, band lists, etc. anywhere in the site. It would be helpful for featuring bands on specific pages or creating separate pages for each band (not very efficient, but some users approach things this way). The shortcode would generate HTML, which would be tied to a provided CSS file that would provide a nice default view of the desired data. Everything would be contained within the plugin files and nothing would need to be done with the theme.
Template Files
The plugin could also ship with template files. The template files could be marked up and styled for a nice default view. You could provide instructions for your user to move the files to the theme folder so that the theme will find the right templates when the post types are viewed. You could even go so far as providing an interface to allow the user to move the files with a single click (note: I would not create files in the user’s theme folder on activation because adding files to their theme without them initiating it is evil).
You can also use filters to utilize these files without moving them out of the plugin folder, keeping everything self contained. I’ve seen the “template_include” and the “{$type}_template” filters used for this purpose. In fact, you could use templates from the themes folder and if they are not present, you can fall back on these filters to provide the default views.
The Question
I like to know what others think are the best practices for these situations, if the presented ideas are problematic in any way, and any alternatives that I’ve not included.
Thank you!