When you develop a plugin in WordPress, maybe you have to create a setting page for your plugin or some pages for special reasons and after your page is done, you need to create a link on the menu to go into that page.
This is a common way to add a custom menu:
Step 1: Create your setting page
You can create a function or a php file that stores your html elements.
Step 2: Create a function and paste this code
1 |
add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $position ); |
Step 3: Hook to admin_menu
action
Here is the example:
1 2 3 4 5 6 7 8 9 10 11 |
add_action( 'admin_menu', 'my_custom_menu' ); function my_custom_menu() { add_menu_page( 'Custom Page Title', 'Custom Menu Title', 'manage_options', 'my-page', 'my_page', '', 6 ); } function my_page() { echo 'Settings Page'; } |
Notice: if the position parameter is omitted, your custom menu will appear at the bottom of the admin menu. Here is the list of default menus position:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
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 |
And be careful if your custom menu uses the same position attribute with an existed menu, it will overwrite this one.
I hope this article helped you learn how to add custom menu on WordPress admin menu.