In this short tutorial, we’ll show you ways to add various menus to the WordPress admin dashboard.
Add menu item hook
This code snippet will add a menu menu item to the left side of the wordpress admin dashboard using the method add_menu_page
.
public static function admin_menu() { add_menu_page(__('Menu 1','menu-test'), __('Menu 1','menu-test'), '', 'mt-top-level-handle','menu title', plugins_url( 'Menu/favicon.png' ), null); }
Adding menus with submenus which hovers out
The code below will create a submenu attached to the main menu which hovers out on mouseover. Using the add_submenu_page()
method.
add_submenu_page('mt-top-level-handle', __('menu 2','menu-test'), __('menu 2','menu-test'), 'manage_options', 'menu_2', array ( __CLASS__, 'menu_2' ));
Removing Admin Menu item
These methods will remove items from the menu. Using the remove_submenu_page()
method.
remove_menu_page(string $menu_slug); remove_submenu_page( 'themes.php', 'widgets.php' );
Wrapping the menu system into its own method and invoke with wordpress hooks
The code below, will organise the menu system into its own method and using add_action()
add_action( 'admin_init', array( 'Admin', 'admin_init' ) ); add_action( 'admin_menu', array( 'Admin', 'admin_menu' ), 5 ); /** * Create the Menu System */ public static function admin_menu() { add_menu_page(__('Admin','menu-test'), __('Admin','menu-test'), '', 'mt-top-level-handle','statistics', plugins_url( 'Admin/favicon.png' ), null); add_submenu_page('mt-top-level-handle', __('Admin Summary','menu-test'), __('Visitor Summary','menu-test'), 'manage_options', 'Admin_stats', array ( __CLASS__, 'Admin_stats' )); add_submenu_page('mt-top-level-handle', __('Admin Configuration','menu-test'), __('Configuration','menu-test'), 'manage_options', 'Admin_configuration', array ( __CLASS__, 'Admin_configuration' )); add_submenu_page('mt-top-level-handle', __('Admin FAQ','menu-test'), __('FAQ','menu-test'), 'manage_options', 'Admin_faq', array ( __CLASS__, 'Admin_faq' )); }