How to overwrite the category template in a plugin

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;
}

Topic archive-template categories plugin-development archives plugins Wordpress

Category Web


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)

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.