1

When developing a plugin that contains a custom post type, for example, what's the best practice for making sure the content will display properly within any theme?

Let's say I want to display my plugin's Event CPT and its details using the exact look of single post (single.php template) – I just want to display additional information from the post meta. I can't simply create a single-post_type.php file, grab header, footer and sidebar and use my own markup since themes use different markup for their single.php template.

What I want is closer to somehow hooking into the loop for that single.php but displaying additional information based on my plugin's custom fields.

Similarly, how would I go around creating an archive template for it (looking like theme's archive.php but with additional info)?

Lastly – what about a separate page template – similar to WooCommerce does with pages like Cart and Checkout – they use theme's header/footer but display their own content on the page.

In other words – how to create a theme-agnostic plugin?

  • 1
    Doing like WooCommerce is a good idea. On the filter template_include, you set the template file to show and in this filter you use the function locate_template to search the template in the theme and use the template of the plugin if it not exists in the theme. Then you have a system where users can override the default template. – mmm Dec 7 '17 at 18:41
  • @mmm – That still requires creating an entire page template, which will have different markup than theme files. I'm looking for a way to add my plugin's content within the loop, for ANY theme so that it will always display properly, regardless of theme used. The simplest example: I have an Event CPT and want to add my plugin's post meta 'start_date' in the loop. – Jusi Dec 7 '17 at 20:14
0

As you have no idea what the theme templates look like, any attempt to build a 'neutral' template is likely to screw up at some point. So your best bet is to hook into elements of the single and archive pages (which may have no separate template - maybe it's a one template theme).

Really exotic themes excepted there are probably two hooks you can count on to be present: the_title and the_content. You would load your content like this:

add_filter ('the_content','wpse288033_add_to_content')
function: wpse288033_add_to_content ($content) {
  if ( 'book' == get_post_type() ) { // or any other test relating to your plugin
    $my_html = // build html from your custom fields
    }
  return $content . $my_html;

Now, because your html is inside the post content the styling of the content will be applied to it.

This approach is reasonably fool proof, but fairly limited. You might prefer to load your custom fields in the metadata section of the theme, using the the_meta_key hook but you cannot be sure if it's there. There are more likely to be author, category, tag and date fields, but that could give some weird results, because theme developers tend to reserve limited space for those. Still, if your custom post type its own taxonomy, hooking into the category might be your best option.

At this point the best approach is probably to build an options page for your plugin that allows users to choose where they want to hook the different data elements of your plugin into your pages.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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