Pawan Kumawat

How To Add Menu In WordPress Admin Panel

How to add menu in WordPress admin panel – In this post we see how we can add custom menu to admin sidebar. Sometimes when we on WordPress 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

This can be done simply by 3 steps :

 

Step 1 : Add Function for adding menu in wordpress. Using ‘add_menu_page’ Function.

Creating menu –

 

add_action('admin_menu', 'new_sample_menu');

 

Step 2 : Integrate That Function to the admin menu using ‘admin_menu’ hook.

 

add_action('admin_menu', 'new_sample_menu');
function new_sample_menu()
{
   //add_menu_page('Page Title','Menu Title','edit_posts','menu_slug','page_callback_function','dashicons-media-spreadsheet');
   add_menu_page('Sample Page', 'Sample Page', 'read', 'sample-page', 'sample_page', '', 10);
}

In new_sample_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/

Step 3 : Add Function to show content or file to the newly menu added function.

 

add_action('admin_menu', 'new_sample_menu');
function new_sample_menu()
{

  //add_menu_page('Page Title','Menu Title','edit_posts','menu_slug','page_callback_function','dashicons-media-spreadsheet');
  add_menu_page('Sample Page', 'Sample Page', 'read', 'sample-page', 'sample_page', '', 10);
}
function sample_page()
{
    //If You Want To Show Content Only
    echo 'Sample Content';
    //If You Want To Show Content From A File
    include ('sample-page.php'); // Path To file
}

 

Reference – https://themes.artbees.net/blog/wordpress-custom-admin-pages/

Enjoy

Share This On :

Leave a Comment

Your email address will not be published. Required fields are marked *

Categories
Scroll to Top

Request A Quote