admin管理员组文章数量:1336199
My theme has an override for /wp-content/themes/THEMENAME/woocommerce/loop/loop-start.php. I want to override this file using a plugin. I understand it can be done using a child theme, but I want to use a plugin (because I have many other modifications bundled inside 1 plugin).
I found some other simular questions on stack exchange but couldn't get to a working solution. Here are my attempts:
Using theme_file_path
:
add_filter( 'theme_file_path', 'override_woocommerce_loop_template', 9999);
function override_woocommerce_loop_template( $path, $file = '' ) {
if( 'woocommerce/loop/loop-start.php' === $file ) {
$path = plugin_dir_path( __FILE__ ) . 'includes/woocommerce/loop/loop-start.php';
}
return $path;
}
Using template_include
:
add_filter('template_include', 'override_woocommerce_loop_template', 9999);
function override_woocommerce_loop_template($template){
if( 'woocommerce/loop/loop-start.php' === $template ) {
$template = plugin_dir_path( __FILE__ ) . 'includes/woocommerce/loop/loop-start.php';
}
return $template;
}
Any hints where I am wrong? Many thanks! :)
My theme has an override for /wp-content/themes/THEMENAME/woocommerce/loop/loop-start.php. I want to override this file using a plugin. I understand it can be done using a child theme, but I want to use a plugin (because I have many other modifications bundled inside 1 plugin).
I found some other simular questions on stack exchange but couldn't get to a working solution. Here are my attempts:
Using theme_file_path
:
add_filter( 'theme_file_path', 'override_woocommerce_loop_template', 9999);
function override_woocommerce_loop_template( $path, $file = '' ) {
if( 'woocommerce/loop/loop-start.php' === $file ) {
$path = plugin_dir_path( __FILE__ ) . 'includes/woocommerce/loop/loop-start.php';
}
return $path;
}
Using template_include
:
add_filter('template_include', 'override_woocommerce_loop_template', 9999);
function override_woocommerce_loop_template($template){
if( 'woocommerce/loop/loop-start.php' === $template ) {
$template = plugin_dir_path( __FILE__ ) . 'includes/woocommerce/loop/loop-start.php';
}
return $template;
}
Any hints where I am wrong? Many thanks! :)
Share Improve this question edited Jun 4, 2020 at 14:33 TVBZ asked Jun 3, 2020 at 20:42 TVBZTVBZ 1298 bronze badges 2 |1 Answer
Reset to default 0You are missing an important part.
Within the template_include hook, you need to do a check, if the other template file, which you would like to overwrite, exists. Then add a logic according to your plugin.
Also, you are messing up the path, by declaring it first, then using plugin_dir_path again in the output, so that is also a bit wrong. You need to pass in the variable for the original template, then - catch it - before delivering it to the output.
I have done this in the past via the "template_include" hook. A filter may also work, though I cannot confirm that.
This is my solution to this, which works in different situations for overwriting a theme file with a plugin file. I haven't tested this, but it should work.
<?php
function wpse_template_logic( $original_template ) {
// Let's build a directory variable for the paths
$dir = plugins_url('/your-plugin-folder-name');
// Check the theme template for wc loop-start.php
// Make sure you establish the correct paths by "get_template_directory_uri()" and "plugins_url()" function
// Also, be sure to "only" trigger the action, when a certain condition is met
// You don't want to trigger this every time, just when the template is requested
if (is_page('whatever_wc_page') || $whatever_condition_you_want_to_check) {
// Now check to see, if the file in the folder XY exists, then overwrite it
// The outer file_exists check will return true or false, if or if not the template exists in your theme folder, if it does, then move to the plugin folder to get the template
// This logic is up to you more or less, according to your strategy
// use the $dir variable to get to the your directory and the template
if(file_exists(get_template_directory_uri().'/woocommerce/loop/loop-start.php')){
return $dir . '/custom-wc-template-folder-in-your-plugin/loop-start.php';
}
return $original_template;
}
// First argument is the hook, then the function name, which you want to execute, then the priority, then the amount of arguments
add_action( 'template_include', 'wpse_template_logic', 10, 1 );
For problems like this in development, I'd highly recomment installing the plugin "What the file". It will show you the template name, which is currently used. Another good plugin to debug things like this is the "Query Monitor". It also has functions to show you the path of things (Themes and plugins) also - very, very useful. Maybe that helps.
I hope I could help a bit. In case it worked i'd be happy if you accept the answer as correct, thank you.
本文标签: Override woocommerce loopstartphp from theme using plugin
版权声明:本文标题:Override woocommerce loop-start.php from theme using plugin? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742374804a2462948.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
template_include
filter hook with a high priority. – Howdy_McGee ♦ Commented Jun 3, 2020 at 22:39