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

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');
| improve this answer | |
  • I have basically this code but after converting my theme to OOP this doesn't work from inside of a class. – Adam Patterson May 16 '19 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.

| improve this answer | |
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;
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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