0

So I basically need to overwrite my category template in my child theme with the one in my plugin but I can't seem to get it working.

I have checked the files paths and everything and there all correct this seems to be the way of doing it but again Wordpress doesn't seem to be using the template from my plugin.

add_filter( 'taxonomy_template', 'load_new_custom_tax_template');
function load_new_custom_tax_template ($tax_template) {
  if (is_category()) {
    $tax_template = dirname(  __FILE__  ) . '/category.php';
  }
  return $tax_template;
}
3
  • If that’s your main plug-in file, and your plugins category.php template is in the same directory try Wordpress plugin_dir_path(__FILE__ ) . “category.php” instead of php’s dirname. Commented Feb 8, 2022 at 16:24
  • @BenHartLenn Yes it is in the main plugin directory, I did try the above but unfortunately, no luck its still not using the template in the plugin. Commented Feb 8, 2022 at 16:40
  • @IbrarrKhan did using category_template or template_include work for you? If not, what solution you used?
    – Sally CJ
    Commented Feb 20, 2022 at 8:01

1 Answer 1

0

I noticed that you're using is_category() in your function, which means you're targetting the default/core category taxonomy and not a custom taxonomy, hence you should instead use the category_template hook. E.g.

add_filter( 'category_template', 'load_new_custom_tax_template');
function load_new_custom_tax_template ($tax_template) {
    // I don't check for is_category() because we're using the category_template filter.
    return dirname( __FILE__ ) . '/templates/category.php';
}

Alternatively, you could use the template_include hook which runs after the <type>_template hook such as the category_template and taxonomy_template:

add_filter( 'template_include', 'load_new_custom_tax_template');
function load_new_custom_tax_template ($tax_template) {
    if (is_category()) {
        $tax_template = dirname( __FILE__ ) . '/templates/category.php';
    }
    return $tax_template;
}

Note though, other plugins can also override the template, hence you might want to use a lower priority, i.e. a greater number as the 3rd parameter for add_filter(). E.g. 20 as in add_filter( 'category_template', 'load_new_custom_tax_template', 20)

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.