我是WordPress的OOP新手,可能需要一些帮助来调试这个问题。我一直在寻找答案,并试图调试自己,但可能需要一只手。我正在尝试创建管理菜单项及其页面,但出现以下错误:
Whoops\\Exception\\ErrorException thrown with message "call_user_func_array() expects parameter 1 to be a valid callback, function \'create_admin_page\' not found or invalid function name"
有人能帮我指出正确的方向吗?提前感谢!
这是我的班级。
namespace MyNamespace;
class MyClass {
public function __construct() {
add_action(\'admin_menu\', array($this, \'create_menu_item\'));
//add_action(\'admin_bar_menu\', \'remove_wp_logo\', 999);
//add_filter( \'admin_bar_menu\', \'change_howdy\', 25 );
ddd($this->create_menu_item());
}
//Get all pages
public function get_all_pages() {
$allpages = get_pages( array( \'post_type\' => \'page\', \'posts_per_page\' => \'-1\') );
return $allpages;
}
//Get Parent Page ID
protected function get_location_id() {
$parents = get_page_by_title(\'Locations\');
return $parents->ID;
}
//Get all locations excluding grandchildren
protected function get_top_level_pages() {
$children = get_page_children( $this->get_location_id(), $this->get_all_pages() );
$locations = array();
foreach ($children as $key => $value) {
if ($this->get_location_id() == $value->post_parent) {
$locations[$key] = $this->{$key} = $value;
}
}
return $locations;
}
//Create top level menu item
public function create_menu_item() {
$locations = $this->get_top_level_pages();
foreach ($locations as $key => $value) {
add_menu_page(
$value->post_title,
$value->post_title,
\'manage_options\',
$value->post_name.\'-admin-page.php\',
\'create_admin_page\', //here is where I am calling the next function
\'dashicons-location-alt\',
6
);
}
}
//Create Admin Page -----should be it\'s own class?
public function create_admin_page() {
$children = get_page_children( $this->get_location_id(), $this->get_all_pages() );
?>
<div class="wrap">
<h2>Welcome Child Pages</h2>
</div>
<ul>
<?php
if(!empty($children)){
echo \'<ul>\';
foreach($children as $child){
echo \'<li><a href="\'.get_permalink($child->ID).\'">\'.$child->post_title.\'</a></li>\';
}
echo \'</ul>\';
}
?>
</ul>
<?php
}
最合适的回答,由SO网友:Jacob Peattie 整理而成
在这一行:
add_action(\'admin_menu\', array($this, \'create_menu_item\'));
您正确通过了
create_menu_item
当前类的方法调用的参数
add_action()
作用您只需要对中的回调应用相同的原则
add_menu_page()
功能:
add_menu_page(
$value->post_title,
$value->post_title,
\'manage_options\',
$value->post_name.\'-admin-page.php\',
array( $this, \'create_admin_page\' ), // <-- Here
\'dashicons-location-alt\',
6
);
这将应用于任何具有“回调”作为参数的WordPress函数。您可以阅读有关PHP回调的更多信息
here.