How to Create a Custom Admin Panel in WordPress using add_menu_page() Function

Home » Snippets » How to Create a Custom Admin Panel in WordPress using add_menu_page() Function
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Are you looking to create a custom admin panel for your WordPress site? If so, you’re in luck because WordPress has a built-in function called "add_menu_page" that allows you to add custom pages to the WordPress admin menu. With this powerful function, you can create your own custom admin pages and menus in just a few simple steps. In this article, we’ll show you how to create a custom admin panel with add_menu_page and take your WordPress site to the next level.

					function WPTurbo_custom_admin_panel() {
    add_menu_page(
        esc_html__('Custom Panel', 'wpturbo'),
        esc_html__('Custom Panel', 'wpturbo'),
        'manage_options',
        'wpturbo-custom-panel',
        'wpturbo_custom_panel_callback',
        'dashicons-admin-tools',
        50
    );
}

function wpturbo_custom_panel_callback() {
    ?>
    <div class="wrap">
        <h1><?php esc_html_e( 'Custom Panel', 'wpturbo' ); ?></h1>
        <!-- Add your custom content here -->
    </div>
    <?php
}
add_action( 'admin_menu', 'WPTurbo_custom_admin_panel' );
				

The code snippet provides a way to create a custom admin panel in WordPress using the add_menu_page() function. This function adds a new top-level or sub-level menu item to the WordPress admin menu.

The add_menu_page() function takes the following parameters:

  • $page_title (string): The text that appears in the title tag of the page and in the menu.
  • $menu_title (string): The text that appears in the menu.
  • $capability (string): The capability required to access this menu.
  • $menu_slug (string): The slug name to refer to this menu item.
  • $function (callable): The function to be called to output the content for this menu item.
  • $icon_url (string): The URL of an icon to be used for this menu item.
  • $position (int): The position of the menu item in the menu.

In the code snippet, we define a new function called WPTurbo_custom_admin_panel() that will be responsible for creating the new menu item. Inside this function, we call the add_menu_page() function and pass in the required parameters.

The $function parameter takes the name of a callback function that will be responsible for outputting the content of the custom admin panel. In the code snippet, we define a new function called wpturbo_custom_panel_callback() for that purpose.

The wpturbo_custom_panel_callback() function outputs the HTML markup that is displayed on the custom admin panel. We include an H1 tag with the title of the custom panel, and we leave a space for custom content to be added.

Finally, we hook the WPTurbo_custom_admin_panel() function into the admin_menu action, which ensures that the custom admin panel is added to the WordPress admin menu.

Register an account to save your snippets or go Pro to get more features.