修改functions.php中的WordPress插件功能 [英] Modify Wordpress plugin function in functions.php
问题描述
如果我在原始插件文件中执行该操作,它可以很好地工作,但我宁愿使它在functions.php,以避免更新插件的问题。
我需要改变它:
public static function loop_shop_per_page(){
return get_option('posts_per_page');
$ b $ p
$ b $ p
pre> public static function loop_shop_per_page(){
return'-1';
我将如何在functions.php中创建一个过滤器来做到这一点?
在不改变核心插件代码的情况下,我不认为你可以实现你想要的。
写得很好的插件通常有 apply_filters
方法调用任何可能需要由主题开发人员调整的内容。因此,插件代码应该看起来像这样:
//使用apply_filters()允许重写值...
public static function loop_shop_per_page(){
$ value = get_option('posts_per_page');
返回apply_filters('filter_loop_shop_per_page',$ value);
}
然后,在您的functions.php文件中,您可以更改通过添加一个如下所示的过滤器来实现这一点:
add_filter('filter_loop_shop_per_page','mytheme_loop_shop_per_page');
函数mytheme_loop_shop_per_page($ value){
return -1;
}
不幸的是,除非您编辑插件代码,否则您可能无法做你想做的事。你总是可以在插件中搜索 apply_filters
来查看是否还有其他的东西可以调整以获得相同的结果。
I need to change the value of a function from a Wordpress plugin that I'm using.
If I do it in the original plugin file, it works perfectly, but I'd rather make it in functions.php to avoid problems on plugin update.
I need to change this:
public static function loop_shop_per_page() {
return get_option( 'posts_per_page' );
}
Into this:
public static function loop_shop_per_page() {
return '-1';
}
How would I go to create a filter in functions.php to do just that?
Without changing the core plugin code, I don't think you can achieve what you want.
Well written plugins usually have apply_filters
method called on anything that might need to be tweaked by a theme developer. As such, the plugin code should look something like this:
// Using apply_filters() to allow overriding of values...
public static function loop_shop_per_page() {
$value = get_option( 'posts_per_page' );
return apply_filters('filter_loop_shop_per_page', $value);
}
Then, in your functions.php file, you'd be able to change the value on for that by adding a filter like so:
add_filter('filter_loop_shop_per_page', 'mytheme_loop_shop_per_page');
function mytheme_loop_shop_per_page($value){
return -1;
}
Unfortunately, unless you edit the plugin code you probably won't be able to do what you want. You could always search the plugin for apply_filters
to see if there's anything else you might be able to tweak to achieve the same results.
这篇关于修改functions.php中的WordPress插件功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!