CorePlugin Development

How to add menu in wordpress admin dashboard

By December 9, 2018 No Comments

Create a Menu in WordPress admin panel

In this post we see how we can add custom menu to admin sidebar. Sometimes when we work with plugins or theme and we need to show some features or any information in admin page then we can use this code snippet to create the same.

As we know WordPress provide hook using which we can easily add custom admin menu. So, for this we’ll use ‘admin_menu’ hook and add action to this hook

1. Creating menu –

Add this code in functions.php file of your plugin or theme.

add_action('admin_menu', 'custom_menu');

In above line of code, first parameter is the hook we discuss about, Second parameter is name of callback function. In callback function you have to write what you want to alter in admin menu.

function custom_menu() { 
 
  add_menu_page( 
      'Page Title', 
      'Menu Title', 
      'edit_posts', 
      'menu_slug', 
      'page_callback_function', 
      'dashicons-media-spreadsheet'
 
     );
}

wordpress-plugin-custom-admin-menu

As you can see in custom_menu() function I just used add_menu_page(). This function allow you to create a menu in admin sidebar and map that menu to a page.

In custom_menu() function

  • First parameter is page title. the title tag of the page when the menu is selected..
  • Second parameter is menu title. The text to be used for menu title.
  • Third one is capability, The capability required for this menu to be displayed to the user. You can find a list of all WordPress capabilities here – https://codex.wordpress.org/Roles_and_Capabilities
  • Fourth parameter is menu slug, which is used for creating page URL. Keep this unique.
  • Fifth parameter is page callback function. The function to be called to output the content for this page.
  • Sixth parameter is for icon, either you can provide a URL of image or you can choose predefined WordPress icons. https://developer.wordpress.org/resource/dashicons/

Content Credit: https://webkul.com/blog/how-to-add-menu-in-wordpress-admin-panel/

Leave a Reply