Andy Tran

Add Sub Menu in Custom Post Type

by | Sep 2, 2021 | Wordpress

Before you read how to add sub menu in custom post type menu it is most important, you understand
How to Add Custom Post Type Without Plugin.

Add sub-menu in Custom Post Type

add_submenu_page() this function used to add submenu in Admin Menu Page. And You will remove submenu from wordpress admin using remove_submenu_page().

add_submenu_page() by default takes 7 parameters.


add_submenu_page( 
      string $parent_slug, 
      string $page_title, 
      string $menu_title, 
      string $capability, 
      string $menu_slug, 
      callable $function = '',
      int $position = null 
)

Example to Add Sub-menu in Custom Post Type

Write the following code in your functions.php file to create custom post type.

    • Login to your WordPress Admin Dashboard.
    • Now from the left sidebar go to Appearance -> Theme Editor.
    • Go to functions.php file and write below code.

//admin menu callback function

function coupon_register_submenu_page() {

    //Add Offer Configuration Sub Menu   
    add_submenu_page('edit.php?post_type=coupon', 'coupon configuration', 'Coupon Configuration', "manage_options", 'coupon_configuration', 'couponconfiguration', '');

    //Add Deal Configuration Sub Menu
    add_submenu_page('edit.php?post_type=coupon', 'deal configuration', 'Deal Configuration', "manage_options", 'deal_configuration', 'dealconfiguration', '');

    //Add Custom Social Sharing Sub Menu
    add_submenu_page('edit.php?post_type=coupon', 'deal configuration', 'Social Options', "manage_options", 'social-share', 'social_share_page', '');
    
}
add_action('admin_menu', 'coupon_register_submenu_page');

//add submenu page callback function

function couponconfiguration(){
    echo 'Offer Configuration';
    return;
}

function dealconfiguration(){
    echo 'DEAL Configuration';
    return;
}

function social_share_page(){
    echo 'Social Sharing Configuration';
    return;
}

To get the right menu item to add the submenu, you need to check the URL that it takes you. For example, by clicking on the “Deal Configuration” button, it loads the page: localhost:8080/coupon/wp-admin/edit.php?post_type=coupon. So the item that we will use will be ‘edit.php?post_type=coupon’.
 
add sub-menu in custom post type

In the WordPress admin panel you have a Custom Post Type called “Coupon”, and that’s where we added a submenu like- Coupon Configuration, Deal Configuration & Social Options.
 
To Continue Learning,
you can refer our blog post Add Meta Box in Custom Post Type