好的,这有点具体。我可能会遗漏一些东西,但因为它给我留下了足够的时间,即使我找到了解决办法,我还需要知道是否有办法正确地做到这一点。
基本上我想在一个类中使用add_meta_box(http://codex.wordpress.org/Function_Reference/add_meta_box)。
我在做什么:
//an array variable I am trying to pass in the class to a callback function as a parameter
$the_array = array(
'something',
'my meta box'
);
//a class where everything happens
class some_class {
//public function that has the array and initiates the add_meta_boxes hook
public function add_box($class_array) {
//add meta boxes hook to add the meta box properly
add_action('add_meta_boxes', array($this, 'adding_custom_meta_boxes'), 10, 2);
//passing the array variable to the callback function
do_action('add_meta_boxes',$class_array);
}
//the callback function of the add_meta_boxes hook
public function adding_custom_meta_boxes($class_array) {
add_meta_box('my-meta-box', __($class_array[1]), 'render_my_meta_box', 'page', 'normal', 'default');
}
public function render_my_meta_box(){
//the code to generate the html of the meta box goes here
}
}
$class_var = new some_class();
$class_var->add_box($the_array);
我收到此错误: 致命错误:在C:\ xampp \ ht .....中调用未定义的函数add_meta_box()
但仅当我使用do_action将变量传递给钩子回调函数
时我找到了解决全局变量问题的方法,但是,是否有人知道这样做的正确方法?
我正在尝试从类中创建元框,这种情况发生了。它在课外很好用。有人有什么想法吗?
答案 0 :(得分:1)
public function adding_custom_meta_boxes($class_array) {
add_meta_box('my-meta-box', __($class_array[1]), array($this, 'render_my_meta_box'), 'page', 'normal', 'default');
}
do_action告诉脚本现在执行附加的操作,并且函数add_meta_boxes尚未加载(谷歌wp函数的加载过程)。这就是add_actions / Filters的重点!