I try to write a plugin for WooCommerce (https://github.com/bassjobsen/woocommerce-twitterbootstrap). I want my templates load like this:
- my custom theme / template path / template name
- my custom theme / template
- my plugin / template name
- 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!)
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.