Modify WordPress plugin function in ‘functions.php’

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 file functions.php to avoid problems on plugin update.

Read More

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 file functions.php to do just that?

Related posts

1 comment

  1. Without changing the core plugin code, I don’t think you can achieve what you want.

    Well written plugins usually have the 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.

Comments are closed.