2

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 . '/' . $templatefilename)) {
            $return_template = TEMPLATEPATH . '/' . $templatefilename;
        } else {
            $return_template = $plugindir . '/themefiles/' . $templatefilename;
        }
        do_theme_redirect($return_template);
    }
    if (is_tax('prodcategories')) {
        $templatefilename = 'taxonomy-prodcategories.php';
        if (file_exists(TEMPLATEPATH . '/' . $templatefilename)) {
            $return_template = TEMPLATEPATH . '/' . $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 . '/' . $templatefilename)) {
            $return_template = TEMPLATEPATH . '/' . $templatefilename;
        } else {
            $return_template = $plugindir . '/themefiles/' . $templatefilename;
        }
        return $return_template;
    }
    if (is_tax('prodcategories')) {
        $templatefilename = 'taxonomy-prodcategories.php';
        if (file_exists(TEMPLATEPATH . '/' . $templatefilename)) {
            $return_template = TEMPLATEPATH . '/' . $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

2

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');
1
  • I have basically this code but after converting my theme to OOP this doesn't work from inside of a class. Commented May 16, 2019 at 3:47
1

This is more safeway.

add_filter( 'template_include', 'wpse138858_woocommerce_category_archive_template');


function wpse138858_woocommerce_category_archive_template( $original_template ) {
    // we're loading the template conditionally, 


  global $post;
   if (is_archive() && get_post_type($post)=='ym_package') {
        // you've to create the template you want to use here first
        return get_template_directory().'/archive-ym_package.php';
    } 
    else if(is_archive() && get_post_type($post)=='ym_place') {
        // you've to create the template you want to use here first
        return get_template_directory().'/archive-ym_place.php';
    } 


    else {
        return $original_template;
    }
}

Please note that put this code in function.php to works fine.

0

This work for me, kindly try it, Thanks
Templates is loaded into cpt file, which was located at
custom_plugin -> app -> cpt -> cpt_article.php
Template is located
custom_plugin -> app -> templates

add_filter( 'template_include', 'load_my_custom_template', 99, 1 );
 function load_custom_templates($template) {
   $post_type = 'article';
   if( is_post_type_archive( $post_type ) && file_exists( plugin_dir_path(__FILE__) . 
      'templates/archive_help_lessions.php' ) ){
      $template = trailingslashit( plugin_dir_path( __FILE__ ) .'app/templates' ).'archive_articles.php';
   }
   if ( is_singular( $post_type ) ) {
      $template = trailingslashit( plugin_dir_path( __FILE__ ) .'app/templates' ).'single_article.php';
  }
   return $template;
}

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.