After logging in to the admin Dashboard you can see the menu on the left side and the RTL websites on the right side. This menu contains some basic menus like Appearance, Plugins, Settings, Users, etc.
add_menu_page( string $page_title, string $menu_title, string $capability, string $menu_slug, callable $function = '', string $icon_url = '', int $position = null )
- page_title (Required) The text is to be displayed in the title tags of the page when the menu is selected.
- menu_title (Required) is the title that shows up in the menu.
- capability (Required) The capability required for this menu to be displayed to the user. For example, if it contains some general options for the site, manage_option could be the best choice. This can be used to restrict it to only admins, editors, or authors. Just set it carefully to avoid any possible security issues.
- menu_slug (Required) The slug name that refers to this menu. It should be unique for this menu page. This will be used as a parameter in the URL of your custom admin page.
- function (Optional) The function to be called to show the output content for this page.
- icon_url (Optional) The URL of the icon to be used for this menu. You can use an image, encoded SVG, or dash icons.
- In order to use an image, simply pass the URL of the image.
- Using existing WordPress icons.
- You can use
none
value to leavediv.wp-menu-image
empty, so an icon can be added via CSS. - You can use the SVG file. Convert the SVG file to base64 and add it with
data:image/svg+xml;base64code
.
- position (Optional) The position in the menu order should appear. Here is the list of numbers of default admin menus:
- 2 – Dashboard
- 4 – Separator
- 5 – Posts
- 10 – Media
- 15 – Links
- 20 – Pages
- 25 – Comments
- 59 – Separator
- 60 – Appearance
- 65 – Plugins
- 70 – Users
- 75 – Tools
- 80 – Settings
- 99 – Separator
Open the functions.php file of your theme or add the add_menu_page to your WordPress plugin. To do this we create a function hs_admin_menu to define the menu.
function test_admin_menu() { add_menu_page( string $page_title, string $menu_title, string $capability, string $menu_slug, callable $callback = ”, string $icon_url = ”, int|float $position = null ): string add_menu_page( __( 'Page Title', 'Test Menu' ), __( 'Menu Title', 'Test Menu' ), 'manage_options', 'sample-page', 'test_admin_page_contents', 'dashicons-schedule', 3 ); } add_action( 'admin_menu', 'test_admin_menu' );
function test_admin_page_contents() { ?> <h1> <?php esc_html_e( 'Welcome to my custom admin page.', 'my-plugin-textdomain' ); ?> </h1> <?php }