我不确定如何在__()构造函数中正确地添加动作...
下面是我的代码:
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,但问题是我不知道在这种情况下把$priority放在哪里……
wp codex:
add_action( string $tag, callable $function_to_add, int $priority = 10, int $accepted_args = 1 )
提前感谢!丹尼斯
发布于 2016-09-21 13:08:27
我不确定,但正如codex中所述:
add_action( string $tag, callable $function_to_add, int $priority = 10, int $accepted_args = 1 )
如果第二个参数$function_to_add在类内,则函数add_class需要一个对象。Object为$this参数。$this参数用于回调!
但是第三个和第四个参数只是传递给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-21 14:19:39
可能有一件小事...您的add_action代码行中有一个拼写错误,因为您调用的是"action_wwoocommerce_single_product_summary“(注意woocommerce中多余的”w“)。
https://stackoverflow.com/questions/39616226
复制