WordPress template_include filter not working properly

I am working on a plugin. The plugin make custom post type “product”. First i use template_redirect action for redirection to single and category pages.

Here is my template_redirect code:

add_action("template_redirect", 'my_theme_redirect');
function my_theme_redirect() {

    global $wp;
    $plugindir = dirname(__FILE__);
    //A Specific Custom Post Type
    if ($wp->query_vars["post_type"] == 'product') {
        $templatefilename="single-product.php";
        if (file_exists(TEMPLATEPATH . "https://wordpress.stackexchange.com/" . $templatefilename)) {
            $return_template = TEMPLATEPATH . "https://wordpress.stackexchange.com/" . $templatefilename;
        } else {
            $return_template = $plugindir . '/themefiles/' . $templatefilename;
        }
        do_theme_redirect($return_template);
    }
    if (is_tax('prodcategories')) {
        $templatefilename="taxonomy-prodcategories.php";
        if (file_exists(TEMPLATEPATH . "https://wordpress.stackexchange.com/" . $templatefilename)) {
            $return_template = TEMPLATEPATH . "https://wordpress.stackexchange.com/" . $templatefilename;
        } else {
            $return_template = $plugindir . '/themefiles/' . $templatefilename;
        }
        do_theme_redirect($return_template);
    }
}

function do_theme_redirect($url) {
    global $post, $wp_query;
    if (have_posts()) {
        include($url);
        die();
    } else {
        $wp_query->is_404 = true;
    }
}

Its working perfectly. But now i am trying to use template_include filter but its not working my site goes blank.

Here is template_include Code:

add_filter("template_include", 'my_theme_redirect');
function my_theme_redirect($templatefilename) {

    global $wp;
    $plugindir = dirname(__FILE__);
    //A Specific Custom Post Type
    if ($wp->query_vars["post_type"] == 'product') {
        $templatefilename="single-product.php";
        if (file_exists(TEMPLATEPATH . "https://wordpress.stackexchange.com/" . $templatefilename)) {
            $return_template = TEMPLATEPATH . "https://wordpress.stackexchange.com/" . $templatefilename;
        } else {
            $return_template = $plugindir . '/themefiles/' . $templatefilename;
        }
        return $return_template;
    }
    if (is_tax('prodcategories')) {
        $templatefilename="taxonomy-prodcategories.php";
        if (file_exists(TEMPLATEPATH . "https://wordpress.stackexchange.com/" . $templatefilename)) {
            $return_template = TEMPLATEPATH . "https://wordpress.stackexchange.com/" . $templatefilename;
        } else {
            $return_template = $plugindir . '/themefiles/' . $templatefilename;
        }
        return $return_template;
    }
}

function do_theme_redirect($url) {
    global $post, $wp_query;
    if (have_posts()) {
        include($url);
        die();
    } else {
        $wp_query->is_404 = true;
    }
}

Any suggestions where i go wrong

3 Answers
3

Ok i done by my own.

I delete all above code and write this code. It works perfectly for me

Code:

function template_chooser($template){
    global $wp_query;
    $plugindir = dirname(__FILE__);

    $post_type = get_query_var('post_type');

    if( $post_type == 'product' ){
        return $plugindir . '/themefiles/single-product.php';
    }

    if (is_tax('prodcategories')) {
        return $plugindir . '/themefiles/taxonomy-prodcategories.php';
    }

    return $template;   
}
add_filter('template_include', 'template_chooser');

Leave a Comment