简体   繁体   中英

Override Function In WordPress Plugin Widget

I need to override a setting in a widget included in a WordPress plugin (the plugin is woocommerce). I've found in the plugin's widget code where I need to make the change (for the product_categories widget, I want to add a $cat_args['depth'] = 1; so that I only see the parent categories) and everything works fine, but I want to be able to do it in a way so that if the plugin ever updates, it doesn't wipe out the change. I have a custom theme that I have developed...is there any way to make the change in that theme so that I can still update woocommerce safely?

The only way to do that would be to ask the Widget developer to do something like this:

$cat_args_depth = 1;
$cat_args_depth =  apply_filters('woocommerce_cat_args_depth', $cat_args_depth );
$cat_args['depth'] = $cat_args_depth;

and then you could just use

add_filter( 'woocommerce_cat_args_depth', function( $cat_args_depth ) { 
    $cat_args_depth = 2; // your value
    return $cat_args_depth ;
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM