通过插件覆盖WordPress主题页面 [英] Overwrite wordpress theme page by plugin
问题描述
我想使用自定义的wordpress插件覆盖主题页面,我尝试添加过滤器,但没有结果,我不想直接更改主题,因为这对每个开发人员都是一个坏主意.这是我的插件代码我添加了一个过滤器来检查页面
I want to overwrite a theme page using my custom wordpress plugin , i tried to add filter but without result , i don't want to change directly on the theme because it's bad idea for every developer. this is my plugin code i added a filter to check pages
public function __construct(){
add_filter('theme_file_path','customize_theme_pages', 99, 2 );
}
,我试图用这样的自定义页面替换主题页面
and i tried to replace theme page with my custom page like that
public function customize_theme_pages($path, $file = ''){
if( 'woocommerce/checkout--/form-checkout.php' === $file ) {
return plugin_dir_url( __FILE__ ).'inc/form-checkout.php';
}
return $path;
}
对我来说,这似乎是合乎逻辑的解决方案,但我的网站没有任何更改
it seems logical solution for me , but there is no changes in my site
推荐答案
您可以使用 woocommerce_locate_template
过滤器覆盖插件中的woocommerce模板文件.
You can use the woocommerce_locate_template
filter to override a woocommerce template file from your plugin.
一个简单的示例,假设您要使用插件的 inc/checkout/form-checkout.php
文件覆盖 checkout/form-checkout.php
:
A quick example, assuming you want to override checkout/form-checkout.php
with your plugin's inc/checkout/form-checkout.php
file:
function customize_theme_pages($template, $template_name, $template_path) {
if ($template_name == 'checkout/form-checkout.php') {
$template = plugin_dir_path( __FILE__ ) . 'inc/checkout/form-checkout.php';
}
return $template;
}
add_filter('woocommerce_locate_template', 'customize_theme_pages', 99, 3);
**我已经测试了上面的代码,并且可以在 WordPress 4.9.8 上运行的 WooCommerce版本3.5.0 上运行.
**I have tested above code, and it's working on WooCommerce Version 3.5.0 running on WordPress 4.9.8.
如果要替换标准页面模板,则可以使用 page_template
过滤器.
If you want you replace a standard page template, you can use page_template
filter.
这篇关于通过插件覆盖WordPress主题页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!