那么有没有可能用add_action
?
class class{}
$my_class = new class;
add_action(\'init\', \'my_class\');
那么有没有可能用add_action
?
class class{}
$my_class = new class;
add_action(\'init\', \'my_class\');
您可以这样做:
class testclass {
function test() {
echo \'howdy\';
}
}
add_action(\'wp_head\',array(\'testclass\',\'test\'));
或者这个:$t = new testclass();
add_action(\'wp_head\',array($t,\'test\'));
它不像。。。$t = new testclass();
add_action(\'wp_head\',\'t\');
// or this either, for good measure
$t = new testclass();
add_action(\'wp_head\',array(\'t\'));
。。但我不确定您使用这种模式想要实现什么。您已经实例化了该类,因此构造函数(如果存在)已经运行。如果没有回调方法,我不知道会发生什么。在(已接受的)答案中,第一个示例不正确:
class testclass() {
function test() {
echo \'howdy\';
}
}
add_action( \'wp_head\', array( \'testclass\', \'test\' ) );
这是不正确的,因为方法test
不是静态的。操作中静态调用的正确用法是:
class testclass() {
function static test() {
echo \'howdy\';
}
}
add_action( \'wp_head\', array( \'testclass\', \'test\' ) );
你也可以这样写:add_action( \'wp_head\', \'testclass::test\' );
class MyPluginClass {
public function __construct() {
add_action( \'save_post\', array( $this, \'myplugin_save_posts\' ) );
}
public function myplugin_save_posts() {
// do stuff here...
}
}
$mypluginclass = new MyPluginClass();
我将回顾我以前的WordPress主题,使函数可过滤,并添加函数\\u exists检查。我有没有想过这会导致现有代码出现问题?// Dummy function if ( ! function_exists( \'custom_func\' )) { function custom_func() { $output = \"Something Awesome\"; return apply_filters(\'custom_func