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?
template_include
, you set the template file to show and in this filter you use the functionlocate_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.