2

我不确定如何在 __() 构造函数中正确添加操作...

下面是我的代码:

class helloWorld{ 

    function __construct() {
        add_action( 'woocommerce_single_product_summary', array( $this, 'action_wwoocommerce_single_product_summary' ) );
    }


    function action_woocommerce_single_product_summary()   {
        return '<br /><h1>Hello World</h1>'; 
    }

}

$Hello_World = new helloWorld();

所以,我试图在这里输出函数 action_woocommerce_single_product_summary() -> action_wwoocommerce_single_product_summary 但问题是我不知道在这种情况下将 int $priority 放在哪里......

wp法典:

add_action( string $tag, callable $function_to_add, int $priority = 10, int $accepted_args = 1 )

提前致谢!!!丹尼斯

4

2 回答 2

1

可能是一件小事......您的 add_action 行中有一个错字,因为您正在调用“action_wwoocommerce_single_product_summary”(请注意 woocommerce 中的额外“w”)。

于 2016-09-21T14:19:38.710 回答
1

我不确定,但来自 codex 的以下内容:

add_action( string $tag, callable $function_to_add, int $priority = 10, int $accepted_args = 1 )

如果第二个参数 $function_to_add 在类内,那么函数 add_class 需要一个对象。对象是 $this 参数。$this 参数用于回调!

但是第 3 和第 4 个参数只是传递给 add_action 函数的变量。

总之

//i think that this works fine
add_action( 'woocommerce_single_product_summary', array( $this, 'action_woocommerce_single_product_summary' ), 10, 1);
于 2016-09-21T13:08:26.593 回答