4

I have a plugin that I am developing that is using custom post types and taxonomies. My question is this. How can I load the content/theme data from my plugin on to the page when the go to the custom url of the taxonomy?

EDIT

I am trying to use the plugin's template files and not the themes for my custom taxomony.

2
  • 1
    Could you be more specific, are you trying to get WordPress to use the plug-in's template files rather than their themes? Apr 25, 2012 at 15:51
  • @StephenHarris Yes that is exactly what I am trying to do.
    – brenjt
    Apr 25, 2012 at 15:54

2 Answers 2

9

First of all - plugins are for generating content, themes are for displaying it. So really, a plugin shouldn't do this. But there are grey areas - for example in an 'events' related plugin, it would be desirable to display dates, venue etc - things that a WordPress theme wouldn't normally display.

I would suggest

  • making the plugin templates over-rideable with templates of the same name in the theme/child theme.
  • Being able to 'turn off' plugin forcing of the template.

To alter the template being used you can use the template_include filter. This is an example for taxonomy templates, but a similar process is possible for custom post types.

add_filter('template_include', 'wpse50201_set_template');
function wpse50201_set_template( $template ){

    //Add option for plugin to turn this off? If so just return $template

    //Check if the taxonomy is being viewed 
    //Suggested: check also if the current template is 'suitable'

    if( is_tax('event-venue') && !wpse50201_is_template($template))
        $template = plugin_dir_url(__FILE__ ).'templates/taxonomy-event-venue.php';

    return $template;
}

Note it assumes that the plugin templates are in a template sub-folder relative to current director.

Logic

This simply checks that the 'event-venue' taxonomy is being viewed. If not it will use the original template.

The wpse50201_is_template function will check if the template WordPress has picked from the theme/child-theme is called taxonomy-event-venue.php or taxonomy-event-venue-{term-slug}.php. If it is - the original template will be used.

This allows users of your plugin to copy them into their theme and edit them, and the plugin will prioritize the theme/child-theme templates. Only if it can't find them does it fall back to the plug-in templates.

function wpse50201_is_template( $template_path ){

    //Get template name
    $template = basename($template_path);

    //Check if template is taxonomy-event-venue.php
    //Check if template is taxonomy-event-venue-{term-slug}.php
    if( 1 == preg_match('/^taxonomy-event-venue((-(\S*))?).php/',$template) )
         return true;

    return false;
}

I've used this method in a plugin - you can see a working example of the above here.

4
  • thanks for the answer. Trying this out it overwrites the entire theme including the head and foot. How can I get it to use the theme but the body content is replaced with mine?
    – brenjt
    Apr 25, 2012 at 16:45
  • 1
    you just call get_header() and get_footer() in the custom template. Apr 25, 2012 at 16:48
  • Perfect. that worked just as I had hoped. Thank you!! I was wondering if this was even possible
    – brenjt
    Apr 25, 2012 at 17:53
  • I think it might be better to use plugin_dir_path( __FILE__) instead, otherwise you might run into issues with allow_url_include
    – jost21
    Feb 23, 2021 at 8:33
2

This is how I'm calling a taxonomy template from a subdirectory within my theme folder. Keep in mind that taxonomy.php will need to stay in your root theme directory.

function call_taxonomy_template_from_directory(){
    global $post;
    $taxonomy_slug = get_query_var('taxonomy');
    load_template(get_template_directory() . "/templates-taxonomy/taxonomy-$taxonomy_slug.php");
}
add_filter('taxonomy_template', 'call_taxonomy_template_from_directory');

For example, my taxonomy is called 'news-category'. The template is located at wp-content/themes/mytheme/templates-taxonomy/taxonomy-news-category.php

1
  • The OP's question is how to load these from a Plugin, and this solution does not work for those. It likely does work when templates are loaded from a Theme.
    – Tami
    Jul 3, 2022 at 13:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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