1

I try to write a plugin for WooCommerce (https://github.com/bassjobsen/woocommerce-twitterbootstrap). I want my templates load like this:

  1. my custom theme / template path / template name
  2. my custom theme / template
  3. my plugin / template name
  4. woocommerce / template name

So when the template not exists in the theme folder it is load from my plugin folder (if exists) instead of the Woocommece folder. On http://www.skyverge.com/blog/override-woocommerce-template-file-within-a-plugin/ if found a working solution for templates load by woocommerce_get_template. But this will not work for woocommerce_get_template_part cause this function call locate_template instead of woocommerce_locate_template (is this a bug?).

Question: I want to know how to overwrite / manipulate locate_template() so it try to load the files from my plugin folder first. Or any other solution to do this.

update thanks to @jesper i cloud load templates now:

function 

my_template_redirect(){
   //pages you want to make true, ex. is_shop()
   global $woocommerce;
   if(is_shop()) {

       $plugin_dir = WP_PLUGIN_DIR.'/'.str_replace( basename( __FILE__), "", plugin_basename(__FILE__) );

    var_dump($plugin_dir);

    load_template($plugin_dir . '/templates/archive-product.php');
   }
}

add_action('template_redirect','my_template_redirect');

/templates/archive-product.php includes content-product.php from the same plugin template dir. content-product.php makes use of the global $product which is null in this case. Do i need to load a other function to set $product?

The above also gives my an other error: PHP Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'woocommerce_before_main_content_grid' not found or invalid function name in /home/bass/Dropbox/testomgeving/jamedotheme/wordpress/wp-includes/plugin.php

With the above code i could write a function to conditional include files in templates/archive-product.php (so solved!)

1
  • 1
    At this time locate_template has no filter nor action hooks and is not a pluggable function: you cannot overwrite / manipulate it without change the core file wp-includes/template.php.
    – gmazzap
    Aug 19, 2013 at 2:10

1 Answer 1

1

I think you can use template redirect action.

I don't test it but it should work:

function my_template_redirect(){
   //pages you want to make true, ex. is_shop()
   if(is_shop()) {
       $plugin_dir = WP_PLUGIN_DIR.'/'.str_replace( basename( __FILE__), "", plugin_basename(__FILE__) );
    load_template($plugin_dir . 'your-template.php');
   }
}

add_action('template_redirect','my_template_redirect');
4
  • thanks. This does not seems to set $name. $name will be always empty. Aug 19, 2013 at 7:18
  • I copied from a plugin I coded, check it now, change de variable to the name of your template.
    – jepser
    Aug 19, 2013 at 17:50
  • Thanks. It will work some way. If will have to check if i need some conditional use of this action. Now i found the template twice (one on the right place and one on the end of the source). Aug 19, 2013 at 18:36
  • hi @jesper thanks. Yes i can load templates now, but the product var needed for the loop is not set, see update of my question Aug 19, 2013 at 22:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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