从 WordPress 中的插件覆盖主题功能 [英] Overriding a theme function from plugin in WordPress
问题描述
是否可以从插件覆盖主题中的功能?我一直在努力寻找如何实现上述目标的示例.
Is it possible to override a function in a theme, from a plugin? I've been struggling to find an example of how I would achieve the above.
如果有人能提供任何线索,那将非常有帮助.
If anyone could shed any light it would be really helpful.
添加一些代码
这是我的功能,它允许 woocommerce 中的购物车使用 ajax 进行更新:
So this is my function which allows the cart in woocommerce to update using ajax:
add_filter('add_to_cart_fragments', 'woocommerce_header_add_to_cartplus_fragment', '1');
function woocommerce_header_add_to_cartplus_fragment( $fragments ) {
global $woocommerce;
$basket_icon = esc_attr($instance['basket_icon']);
ob_start();
?>
<a class="cartplus-contents" href="<?php echo $woocommerce->cart->get_cart_url(); ?>" onclick="javscript: return false;" title="<?php _e('View your shopping cart', 'woothemes'); ?>"><?php echo sprintf(_n('%d item', '%d items', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count);?> - <?php echo $woocommerce->cart->get_cart_total(); ?></a>
<?php
$fragments['a.cartplus-contents'] = ob_get_clean();
return $fragments;
}
然而,一些主题也有类似的内置功能 - 在这些主题中,我的代码停止工作,这看起来很奇怪,因为我在我的插件中两次使用上述代码(一个是上述代码的变体),并且它们一起工作得很好.这是主题内置的代码:
However, some themes also have a similar function built in - in these themes my code stops working which seems strange as I use the above code twice in my plugin (one is a variation of the above) and they work fine together. This is the code built in to the theme:
add_filter('add_to_cart_fragments', 'woocommerce_cart_link');
function woocommerce_cart_link() {
global $woocommerce;
ob_start();
?>
<a href="<?php echo $woocommerce->cart->get_cart_url(); ?>" title="<?php echo sprintf(_n('%d item', '%d items', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count);?> <?php _e('in your shopping cart', 'woothemes'); ?>" class="cart-button ">
<span class="label"><?php _e('My Basket:', 'woothemes'); ?></span>
<?php echo $woocommerce->cart->get_cart_total(); ?>
<span class="items"><?php echo sprintf(_n('%d item', '%d items', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count); ?></span>
</a>
<?php
$fragments['a.cart-button'] = ob_get_clean();
return $fragments;
}
推荐答案
除非该函数旨在被覆盖,否则不会.这是基本的 PHP.你不能重新定义一个函数.如果你尝试,你会得到一个致命的错误.
Unless the function is meant to be overridden, no. This is basic PHP. You can't redefine a function. If you try you will get a fatal error.
部分 WordPress 被写入将被覆盖.查看 /wp-includes/pluggable.php
.其中的每个函数都包含在 if( !function_exists(...) )
条件中.除非你的主题做了同样的事情,而且有些是为了某些功能,否则你不能覆盖.
Parts of WordPress are written to be overwritten. Look at /wp-includes/pluggable.php
. Every function in there is wrapped in a if( !function_exists(...) )
conditional. Unless your theme did the same, and some do for some functions, you can't overwrite.
四处寻找可能对您有所帮助的过滤器.
Look around for filters that might help you instead.
看看你的代码,你应该能够解开它.只要确保足够晚地钩开钩.这不是一个好的解决方案,但因为您正在破坏主题功能,并且还必须知道主题正在使用的所有挂钩函数的名称.
Looking at your code, you should be able to unhook that. Just make sure to hook the unhook late enough. That is not a good solution, though since you are breaking theme functionality and also must know the know the names of all the hooked functions that themes are using.
在 $fragments
或 $_POST
或 $_GET
或其他任何东西中是否有一些东西可以用来有条件地运行你的代码,剩下的就别管了.
Is there something in $fragments
, or in $_POST
or $_GET
or anything else, that you can use to conditionally run your code, leaving the rest alone.
这篇关于从 WordPress 中的插件覆盖主题功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!