我在现有的插件中有以下功能:
public static function init() {
add_filter( 'wcs_view_subscription_actions', __CLASS__ . '::add_edit_address_subscription_action', 10, 2 );
}
public static function add_edit_address_subscription_action( $actions, $subscription ) {
if ( $subscription->needs_shipping_address() && $subscription->has_status( array( 'active', 'on-hold' ) ) ) {
$actions['change_address'] = array(
'url' => add_query_arg( array( 'subscription' => $subscription->get_id() ), wc_get_endpoint_url( 'edit-address', 'shipping' ) ),
'name' => __( 'Change Address', 'woocommerce-subscriptions' ),
);
}
return $actions;
}我试图修改它,这样我就可以向$actions数组中添加一些内容。在不直接修改插件的情况下,这是可能的吗?我可以通过过滤functions.php文件来实现吗?
发布于 2018-08-14 16:55:47
您可以简单地在优先级较低或较高的参数中使用相同的筛选器对$actions数组进行适当的更改。这样,您就可以创建一个小的自定义插件(或者修改主题的functions.php文件),而不必直接修改现有的插件。
例如:如果您希望在add_edit_address_subscription_action函数之后执行自定义代码,那么对wcs_view_subscription_actions筛选器使用一个更大的优先级参数(较低的优先级)。
示例代码(将其作为自定义插件的一部分或在主题的functions.php文件中使用):
// original filter uses priority 10, so priority 11 will make sure that this function executes after the original implementation
add_filter( 'wcs_view_subscription_actions', 'wpse_custom_action_after', 11, 2 );
function wpse_custom_action_after( $actions, $subscription ) {
// your custom changes to $actions array HERE
// this will be executed after add_edit_address_subscription_action function
return $actions;
}另一方面,如果您希望在add_edit_address_subscription_action函数之前执行自定义代码,则使用较小的优先级参数(更高的优先级)。
示例代码(将其作为自定义插件的一部分或在主题的functions.php文件中使用):
// original filter uses priority 10, so priority 9 will make sure that this function executes before the original implementation
add_filter( 'wcs_view_subscription_actions', 'wpse_custom_action_before', 9, 2 );
function wpse_custom_action_before( $actions, $subscription ) {
// your custom changes to $actions array HERE
// this will be executed before add_edit_address_subscription_action function
return $actions;
}发布于 2018-08-14 16:55:58
是的,您可以用自己的函数在functions.php中更改D1数组。
function your_function_name( $actions, $subscription ) {
// here you can modify $actions array
//
return $actions;
}
add_filter( 'wcs_view_subscription_actions', 'your_function_name', 15, 2 );https://wordpress.stackexchange.com/questions/311438
复制相似问题