在WordPress中,大多数函数直到事件堆栈中的某些点才会加载。在WordPress的事件驱动架构(EDA)中,您必须将类放入操作或过滤器中。
class MyGreatClass{
public function __construct(){
//I can use WP functions here
}
public function myGreatMethod(){
return "I don\'t do much, but the WP functions are available to me!";
}
}
//This line instantiates MyGreatClass and calls myGreatMethod during the "init" event.
add_action(\'init\', array(new MyGreatClass, myGreatFunction\'));