Last updated on September 13, 2023

Change Admin Menu

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Add or remove items in the WordPress admin menu.

Modifying the WordPress admin menu can help you organize your admin pages and provide quick access to the most important pages.

Remove items from the WordPress admin menu

Here’s an example code snippet that demonstrates how to remove items from the WordPress admin menu pages:

function wpsnippets_admin_menu() {

    // Remove the default Posts and Comments menu items
    remove_menu_page( 'edit.php' );
    remove_menu_page( 'edit-comments.php' );
    
}

add_action( 'admin_menu', 'wpsnippets_admin_menu' );

In the code snippet above, we define a function called wpsnippets_admin_menu that uses the remove_menu_page function to remove the default Posts and Comments menu items from the WordPress admin menu.

Add items to the WordPress admin menu

Here’s an example code snippet that demonstrates how to add items from the WordPress admin menu pages:

function wpsnippets_admin_menu() {
    
    // Add a new menu item for a custom post type
    add_menu_page( 
        __( 'Custom page title', 'wpsnippets' ), 
        __( 'Custom menu title', 'wpsnippets' ), 
        'manage_options', 
        'my-plugin-admin-page-slug',
        'my_plugin_admin_page', 
        'dashicons-admin-post', 
        6 );
}

add_action( 'admin_menu', 'wpsnippets_admin_menu' );

/**
 * Callback function to display the content of the custom admin page
 */
function my_plugin_admin_page() {
     // Custom admin page content goes here
}

In the example above, we use the add_menu_page function to add a new menu item for a custom page. This function takes several parameters, including the title of the page of the menu item, the item’s label in the menu, the required user capability, the slug of the menu item, the callback function for rendering the page’s content, the icon to use for the menu item, and the position in the menu. In this example, we set the required user capability to manage_options so that only administrators can access this menu item. We also set the position of the menu item to 6, which places it below the Media menu item. The content of the page will be rendered by the my_plugin_admin_page() callback function, specified by the 5th parameter.

Once you have added this code snippet to your theme’s functions.php file, the WordPress admin menu will reflect the changes you made. You can modify this code to add or remove other menu items as needed.

Last updated on September 13, 2023. Originally posted on May 3, 2023.

Leave a Reply

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