Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

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

1 Answer 1

up vote 4 down vote accepted

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

I would suggest

  • making the plug-in templates over-rideable with templates of the same name in the theme/child theme.
  • Being able to 'turn off' plug-in 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 plug-in 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 plug-in 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_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 plug to copy them into their theme and edit them, and the plug-in will prioritise 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 plug-in - you can see a working example of the above here.

share|improve this answer
    
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 '12 at 16:45
    
you just call get_header() and get_footer() in the custom template. –  Stephen Harris Apr 25 '12 at 16:48
    
Perfect. that worked just as I had hoped. Thank you!! I was wondering if this was even possible –  brenjt Apr 25 '12 at 17:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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