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 two custom post types (e.g. post_type_1 and post_type_2) that I would like to redirect to independent templates (single-post_type_1.php and single-post_type_2.php) to handle their display. I don't want to put the display templates in the theme folder as I want them self-contained in their respective plugin folders.

How can I have each of them register a template_redirect hook without affecting the other? Or should I be using a different technique?

Currently, I'm doing this in Plugin 1:

add_action( 'template_redirect', 'template_redirect_1' );
function template_redirect_1() {
    global $wp_query;
    global $wp;

    if ( $wp_query->query_vars['post_type'] === 'post_type_1' ) {

        if ( have_posts() )
        {
            include( PATH_TO_PLUGIN_1 . '/views/single-post_type_1.php' );
            die();
        }
        else
        {
            $wp_query->is_404 = true;
        }

    }
}

And this in Plugin 2:

add_action( 'template_redirect', 'template_redirect_2' );
function template_redirect_2() {
    global $wp_query;
    global $wp;

    if ( $wp_query->query_vars['post_type'] === 'post_type_2' ) {

        if ( have_posts() )
        {
            include( PATH_TO_PLUGIN_2 . '/views/single-post_type_2.php' );
            die();
        }
        else
        {
            $wp_query->is_404 = true;
        }

    }
}

Once I register plugin 2's template_redirect hook, plugin 1's no longer works.

Am I missing something?

What is the best way to do this?

share|improve this question
add comment

1 Answer

up vote 11 down vote accepted

You should be using the template_include filter for this:

add_filter('template_include', 'wpse_44239_template_include', 1, 1);
function wpse_44239_template_include($template){
    global $wp_query;
    //Do your processing here and define $template as the full path to your alt template.
    return $template;
}

template_redirect is the action called directly before headers are sent for the output of the rendered template. It's a convenient hook to do 404 redirects, etc... but shouldn't be used for including other templates paths as WordPress does this innately with the 'template_include' filter.

template_include and single_template hooks deal ONLY with the path of the template used for rendering the content. This is the proper place to adjust a template path.

Update from comment by @ChipBennett:

single_template has been removed as of 3.4. Use {posttype}_template instead.

share|improve this answer
    
So, this works, but it turns out the template_redirect and single_template work too. The real problem I determined was that I have a custom admin column sorting function that hooks into the 'request' filter and I wasn't limiting the modification of the $vars variable to only when post type was of a certain type. But, I'm glad to know about the template_include hook. Still not sure what the difference is between template_redirect, template_include and single_template though. –  anderly Mar 2 '12 at 17:24
    
@anderly I updated my answer. Hope this helps. –  Brian Fegter Mar 2 '12 at 17:36
    
So, what about about single_template? This link (codex.wordpress.org/Plugin_API/Filter_Reference/single_template) says it can be used to adjust the template for a post or page when the single template is called. This is what I'm trying to do. In any case, using either template_include or single_template filters work and appear to accomplish the same thing. –  anderly Mar 2 '12 at 17:39
1  
single_template works the same way except it's called conditionally. This helps to isolate path changes instead of using a hammer filter on the whole site. –  Brian Fegter Mar 2 '12 at 17:42
1  
very cool... i didn't know about template_include –  helgatheviking Mar 2 '12 at 20:39
show 8 more comments

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.