在我的functions.php
我有
require 'autoloader.php';
add_action('init', 'MyClass::make');
在我的MyClass
课上
class MyClass {
public static function make($type) {
var_dump($type);die;
}
}
但var_dump
的输出是string(0) ""
。我想要的是将参数作为$type
传递给这个类。我尝试的是通过do_action
传递它,但我认为这不是最佳做法。
如何将参数传递给add_action
类中的方法?
据Wordpress Documentation说: add_action最多可以包含4个参数,其中第四个是可传递给函数的参数计数。
add_action( 'init', 'MyClass::make', 10 /*Action priority, default is 10*/, 1 );
然后你可以将任何东西作为一个参数传递给函数:
do_action( 'init', "Here I am!" );