1

I'm making a plugin which includes a custom post type with custom taxonomies.

I have a custom template - taxonomy-coupon-category.php - but these template files are specific to a single theme.

What is the best practice to make my plugin include a custom taxonomy/archive template which works for ALL themes?

My custom code only needs to alter the loop and the internals of the loop. The problem is really that the template includes other theme specific html e.g. sidebar location, other stuff.

<?php get_header(); ?>
<!-- my unique content -->
<?php get_footer(); ?>

It'd be great if this code above would work on all themes but it doesn't (e.g. sidebar might be missing, or with roots theme header/footer appears twice) - how do other plugins manage to create custom archives and custom taxonomy templates which work on all themes?

I just need my taxonomy-coupon-category.php file to replace the loop generated content and leave the theme to handle the surrounding html.

2

Creating pages is the job of a theme, plugins should not attempt to do that. What you can do is to provide a shortcode which will add the relevant html when used and let the user add it as part of his content however he wants to use it.

That said, if you really must have something automatic you can use the page.php template of the active theme and supply your own content by using the the_content and the_title filters. This should work for most themes with a simple page design.

  • I've got a shortcode for the top-level archive, but these pages are e.g. /category/category1/ or /category/category2 so shortcodes won't work. I'll take a look at what you've suggested about the_content filter and the_title filter as that sounds v close to what I need. Cheers. – Dave Hilditch Oct 5 '14 at 16:10
  • I'm still struggling to find the suitable filter to apply to override the archive pages. I've removed my custom template for now, now I'm trying to find a filter to apply which would allow me to modify each item of the loop in the archive view when I've conditionally checked that this is my custom post type being viewed. All I can find in the official docs is this codex.wordpress.org/Plugin_API/Filter_Reference/… which references custom templates, no mention of filters – Dave Hilditch Oct 5 '14 at 16:46
  • It looks like this guy has the same problem - and a potential solution - wordpress.stackexchange.com/questions/75307/… – Dave Hilditch Oct 5 '14 at 16:54
  • @DaveHilditch, that answer do not work without knowing the structure of the generated output. As I said in the first sentence of the answer, you should not do it in a plugin. – Mark Kaplun Oct 5 '14 at 17:06
  • Surely other people who have made custom post types have had to alter the archive html? Surely others have done this inside a plugin? My plugin definitely needs to work with all themes but it also definitely needs to alter the loop html. – Dave Hilditch Oct 5 '14 at 17:10

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.