I am building a WordPress plugin based on the WordPress Plugin Boilerplate (Object-Orientated Programming) and I am facing a difficulty when calling the function displaying my plugin admin page in my custom admin dashboard menu.
In my class I call for my custom menu method, and it works as the menu shows up great.
private function define_admin_hooks() {
$plugin_admin = new myPlugin_Admin( $this->get_plugin_name(), $this->get_version() );
$this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_styles' );
$this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_scripts' );
// Load CPTs
$this->loader->add_action( 'admin_init', $plugin_admin, 'create_cpt_person' );
$this->loader->add_action( 'admin_menu', $plugin_admin, 'setup_admin_menus' );
$this->loader->add_action( 'parent_file', $plugin_admin, 'set_current_menu' );
}
And it then executes my setup_admin_menus method.
public static function setup_admin_menus() {
$page_title = 'myPlugin';
$menu_title = 'myPlugin';
$capability = 'manage_options';
$menu_slug = 'myplugin_admin';
$function = 'display_admin_page'; // This is what triggers the showing of my admin page once I click on the parent menu (as if you were clicking on "Pages" in your admin dashboard)
$icon_url = 'dashicons-admin-page';
$position = 3;
add_menu_page(
$page_title,
$menu_title,
$capability,
$menu_slug,
$function,
$icon_url,
$position
);
$submenu_pages = array(
// Post Type :: Person
array(
'parent_slug' => 'myplugin_admin',
'page_title' => 'People',
'menu_title' => 'People',
'capability' => 'read',
'menu_slug' => 'edit.php?post_type=person',
'function' => null
)
);
// Add each submenu item to custom admin menu.
foreach($submenu_pages as $submenu){
add_submenu_page(
$submenu['parent_slug'],
$submenu['page_title'],
$submenu['menu_title'],
$submenu['capability'],
$submenu['menu_slug'],
$submenu['function']
);
}
}
And this is the function called:
public static function display_admin_page() {
echo '<div class="wrap">';
echo '<h2>Welcome To WordPets</h2>';
echo '<p>This is the custom admin page created from the custom admin menu.</p>';
echo '</div><!-- end .wrap -->';
echo '<div class="clear"></div>';
}
But whenever I go to my plugin’s admin page, i get Warning: call_user_func_array() expects parameter 1 to be a valid callback, function ‘display_admin_page’ not found or invalid function name in /Users/Lazhar/dev/wp-includes/plugin.php on line 496 and it does not show anything but the error message.
Any help would be hugely appreciated 🙂
Try to put your
display_admin_page()
method outside the classAnother way (instead of placing function outside of the class) is by passing the class method as array:
This is the proper way of referecing the functions (class methods).