Modify Wordpress plugin function in functions.php

2019-05-23 16:12发布

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?

1条回答
聊天终结者
2楼-- · 2019-05-23 16:36

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.

查看更多
登录 后发表回答