You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WC Teams成员区域钩子参数错误,函数接收参数不足

WooCommerce Teams成员区域钩子参数错误解决

问题背景

要给WooCommerce Memberships for Teams的团队成员区域添加自定义操作按钮,使用的钩子是:

$actions = apply_filters( "wc_memberships_for_teams_teams_area_{$section}_actions", $default_actions, $team, $object );

当$section为members时,钩子名是wc_memberships_for_teams_teams_area_members_actions,该钩子会传递3个参数:$actions、$team、$object。

但编写的调用代码运行时触发参数不足错误:

add_filter( 'wc_memberships_for_teams_teams_area_members_actions',    'add_guideline_action' , 10, 2, 4 );
function add_guideline_action( $actions, $team, $member ) {
    $actions = array_merge( array( 'guidelines' => array(
        'url'  => "/thingy",
        'name' => __( 'Guidelines', 'woocommerce-memberships-for-teams' ),
    ) ), $actions );
    return $actions;
}

错误信息:

Too few arguments to function add_guideline_action(), 2 passed in /var/www/html/wp-includes/class-wp-hook.php on line 312 and exactly 3 expected in /var/www/html/wp-content/themes/hello-theme-child/functions.php:332

错误原因

问题出在add_filter的参数使用错误:

  • WordPress的add_filter标准参数顺序是:add_filter( $hook_name, $callback, $priority = 10, $accepted_args = 1 )
  • 你写的add_filter( ... , 10, 2, 4 )中,第四个参数2是accepted_args(指定回调函数接受的参数数量),但你需要接收3个参数,所以这里应该设为3;多余的第五个参数4完全无效,会被系统忽略。
  • 因为accepted_args被设为2,WordPress只会传递前2个参数给回调函数,而你的回调明确要求3个参数,所以触发了参数不匹配的错误。

另外补充:钩子的第三个参数是$object,你回调里写的是$member,在members分区下$object确实对应团队成员对象,逻辑上没问题,但建议保持参数名称一致,避免后续维护混淆。

修复代码

修正add_filter的参数,将接受的参数数量设置为3,去掉多余参数:

add_filter( 'wc_memberships_for_teams_teams_area_members_actions', 'add_guideline_action', 10, 3 );
function add_guideline_action( $actions, $team, $object ) {
    $actions = array_merge( array( 'guidelines' => array(
        'url'  => "/thingy",
        'name' => __( 'Guidelines', 'woocommerce-memberships-for-teams' ),
    ) ), $actions );
    return $actions;
}

修改后,WordPress会正确传递3个参数给回调函数,错误即可解决。


内容的提问来源于stack exchange,提问作者Will

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 17:07:40