如何在自定义插件中覆盖主题功能?
首先,我的问题:
- 我在
W3C验证器
平台上有一个WooCommerce
错误。 - 我通过重定向该特定文件的
WooCommerce
的模板路径来覆盖文件。
问题:
- 我当前正在使用的
WordPress主题
也与我的自定义插件相同的文件覆盖了相同的文件
覆盖取消我的插件的
覆盖功能。
代码:
这是我的一些代码:
这是我使用内置的WordPress函数添加的函数/functions/add_filter/“ rel =“ nofollow noreferrer”> add_filter()高优先级值为“ 1”,以及文件i的路径i Am覆盖是... wooCommerce/templates/单产品/add-to-to-cart/variable.php
function wc_locate_template( $template, $template_name, $template_path ) {
// Check if WooCommerce is present on the website
if ( empty( $template ) || ! is_callable( 'WC' ) || ( defined( 'WC_TEMPLATE_DEBUG_MODE' ) && WC_TEMPLATE_DEBUG_MODE ) ) {
return $template;
}
// Variables
$default_path = WC()->plugin_path() . '/templates/';
$plugin_path = untrailingslashit( plugin_dir_path( __FILE__ ) ) . '/templates/';
$template_new = str_replace( $default_path, $plugin_path, $template );
// Processing
if (
false !== strpos( $template, $default_path )
&& file_exists( $template_new )
) {
$template = $template_new;
}
// Output
return $template;
}
add_filter( 'woocommerce_locate_template', 'wc_locate_template', 1, 3 );
问题:
我如何确保我的插件以这种方式或这里升的方式覆盖文件,以使主题的覆盖不会重叠?
有用的链接:
谢谢您提前时间。
To begin with, my problem:
- I have a
WooCommerce
error on theW3C Validator
platform. - I overwrite the file by redirecting the template path of
WooCommerce
for that specific file.
Problem:
- The
Wordpress Theme
that I am currently using is also overwritting the same file as myCustom plugin
, as a result theTheme's
overwriting cancels out myplugin's
overwritting functionality.
CODE:
Here's some of my code:
This is the function I add using the built in wordpress function add_filter() with high priority value of "1" plus the path of the file I am overwritting is... woocommerce/templates/single-product/add-to-cart/variable.php
function wc_locate_template( $template, $template_name, $template_path ) {
// Check if WooCommerce is present on the website
if ( empty( $template ) || ! is_callable( 'WC' ) || ( defined( 'WC_TEMPLATE_DEBUG_MODE' ) && WC_TEMPLATE_DEBUG_MODE ) ) {
return $template;
}
// Variables
$default_path = WC()->plugin_path() . '/templates/';
$plugin_path = untrailingslashit( plugin_dir_path( __FILE__ ) ) . '/templates/';
$template_new = str_replace( $default_path, $plugin_path, $template );
// Processing
if (
false !== strpos( $template, $default_path )
&& file_exists( $template_new )
) {
$template = $template_new;
}
// Output
return $template;
}
add_filter( 'woocommerce_locate_template', 'wc_locate_template', 1, 3 );
Questions:
How can I make sure that my plugin will overwrite the file of in such way or prority so that the theme's overwritting won't overlap it?
Useful links:
Thank you in advance for your time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是如何使用插件覆盖WooCommerce模板的一个示例:
然后,您应该能够通过复制要覆盖插件文件夹中的模板来实现目标。
示例:
your_plugin/template/woocommerce/单产品/add-to-to-cart/variable.php
That's an example on how to override a woocommerce template with a plugin:
Then you should be able to reach your goal by copying the template you want to override in your plugin folder.
Example:
your_plugin/template/WooCommerce/single-product/add-to-cart/variable.php
优先级1表示您的函数将首先应用,因此附加到过滤器上的任何其他功能高于1的数字都可以覆盖您的功能。
您需要放置最高的数字,以确保您的功能将是最后一个应用的功能。
使用
php_int_max
使您的功能成为最后一个应用。priority 1 means your function will be applied first, so any other function attached to the filter with a number higher than 1 will simply overwrite your function.
You need to put highest possible number to make sure your function will be the last one applied.
use
PHP_INT_MAX
to make your function the last one applied.挂钩和过滤器是根据优先级运行的修改,
被称为“最终的优先级”将是其最终输出,
这是一个示例
我有此
假设
功能
如果您调用
youfree()
onwp_head
,但是如果您调用该
youfree()
在wp_footer
上,请在您的中情况,
但是,如果您很难注入它,则最好的选择是使用 QUERY MONITE a>
Hooks and filter are modification which runs based on priority,
the highest the priority before its being called will be the its final output
Here's an example
Assuming I have this function
then I have all these filters with different priorities
Here is the output
if you call
youFree()
onwp_head
but if you call that
youFree()
function onwp_footer
So in your situation,
but if your having a hard at where to inject it your best option is to find it using query monitor