How To Create Admin Menu For WordPress Plugin
At first, we will follow the reference of WordPress documentation.
We will use this function in the plugin.
add_menu_page( string $page_title, string $menu_title, string $capability, string $menu_slug, callable $function = '', string $icon_url = '', int $position = null )
add_menu_page() function which takes seven parameters:
$page_title:
(string) (Required) The text to be displayed in the title tags of the page when the menu is selected.
$menu_title:
(string) (Required) The text to be used for the menu.
$capability:
(string) (Required) The capability required for this menu to be displayed to the user.
$menu_slug:
(string) (Required) The slug name to refer to this menu by. Should be unique for this menu page and only include lowercase alphanumeric, dashes, and underscores characters to be compatible with sanitize_key().
$function:
(callable) (Optional) The function to be called to output the content for this page.
Default value: ‘ ‘
$icon_url:
(string) (Optional) The URL to the icon to be used for this menu.
Pass a base64-encoded SVG using a data URI, which will be colored to match the color scheme. This should begin with ‘data:image/svg+xml;base64,’.
Pass the name of a Dashicons helper class to use a font icon, e.g. ‘dashicons-chart-pie’.
Pass ‘none’ to leave div.wp-menu-image empty so an icon can be added via CSS.
Default value: ‘ ‘
$position:
(int) (Optional) The position in the menu order this item should appear.
Default value: null
Now we will create a simple plugin named Top Bar Notice and will create a admin menu. Just place this code inside index.php file in the plugin.
<?php /* Plugin Name: Ari Top Bar Notice Description: This plugin is made for personal usage. Plugin URI: https://www.ainayem.com Version: 1.0 Author: Ariful Author URI: https://www.ainayem.com Text Domain: ari-top-bar-notice */ add_action('admin_menu', 'ari_top_bar_notice_menu'); function ari_top_bar_notice_menu() { $page_title = 'Top Bar Notice'; $menu_title = 'Top Bar Notice'; $capability = 'manage_options'; $menu_slug = 'ari-top-bar-notice'; $function = 'ari_top_bar_notice_page_callback'; $icon_url = 'dashicons-megaphone'; $position = 30; add_menu_page($page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $position); }
Now we can see this menu appearing in the dashboard like this.