How to Add Custom Menu to Admin Sidebar in WordPress?

WordPress Blog Title How to Add Custom Menu to Admin Sidebar in Wordpress

The WordPress admin dashboard is where website owners and developers work and administer their sites. By default WordPress includes an extremely intuitive sidebar menu with the critical features: Posts, Pages, Media, and Plugins. However, at times you may want to add a custom menu item to the sidebar – perhaps a link to a settings page, a custom post type, or even an external tool when building a custom WordPress plugin or theme.

Hire WordPress Developer

 Here, we will provide you with step-by-step instructions to add a custom menu to the WordPress admin sidebar with code examples.

Why Add a Custom Menu to WordPress Admin?

Adding a custom admin menu can be beneficial when:

  • You are building a plugin that requires its own dashboard.
  • You want to provide quick access to your settings or configuration pages.
  • You are managing multiple post types and want to better manage navigation.
  • You want to provide a simple way for clients to manage custom features.

Steps to Add Custom Menu to WordPress Admin Sidebar:

For creating a custom menu in the admin sidebar, we use functions like add_menu_page(), add_action() which are bydefault provided by WordPress.

Step 1: Place below code inside your theme’s functions.php or a custom plugin file:

<?php
// Action hook for admin menu
add_action('admin_menu', 'my_custom_admin_menu');

// Main function to create custom menu
function my_custom_admin_menu() {
    add_menu_page(
        'My First Custom Menu',        // Page title
        'First Menu',              // Menu title
        'manage_options',           // Capability (who can access)
        'first-menu',           // Menu slug
        'my_custom_menu_page',      // Function to display page content
        'dashicons-admin-generic',  // Icon (WordPress Dashicons)
        20                          // Position in the sidebar
    );
}

// Function to display page content
function my_custom_menu_page() {
    echo '<div class="wrap">';
    echo '<h1>Welcome to My Custom Menu</h1>';
    echo '<p>This is a custom admin page in WordPress.</p>';
    echo '</div>';
}

Explanation:

Here, add_menu_page() function will add your custom menu to the admin sidebar. Let’s find out what each parameter inside main function add_menu_page() means:

  • ‘My First Custom Menu’: The text you see at top of the page when the menu is opened.
  • ‘First Menu’: name you will see in admin sidebar.
  • ‘manage_options’: it defines who can access this menu. This ‘manage_options’ means its for role Administrator.
  • ‘first-menu’: it’s a menu slug or you can say, a unique identifier for the menu. It will be used by any URL.
  • ‘my_custom_menu_page()’: it is the callback function to display contents of your custom menu. For example purpose, here just a simple html is used to display page content.
  • ‘dashicons-admin-generic’: it’s the icon which will be displayed in admin sidebar along with the name of sidebar menu.
  • ’20’: it defines in which order your custom menu will appear (lower the number, higher the position).

Conclusion:

Adding a custom menu to the WordPress admin sidebar is a great way to extend functionality and improve usability for site administrators.

FAQ

  1. What is the use of adding a custom menu in the WordPress admin sidebar?

Custom menus give developers and admins the opportunity to quickly access settings, or custom post types, or plugin dashboards, as opposed to many different options.

  1. Can I add icons to custom menus?

Yes. You can use some of the Dashicons provided by WordPress, and also specify custom image URLs for icons.

  1. Do I need coding knowledge to add a custom menu in WordPress?

Yes, you will need at least some basic PHP knowledge in terms of writing functions, and using WordPress hooks like admin_menu.

Previous Article

Laravel Yajra DataTables- Handling Large Datasets Efficiently

Next Article

HTTP 404 Not Found Error: What Is It & How To Fix It?

Write a Comment

Leave a Comment

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

Get Connect With Us

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨