1

I am learning WordPress Plugin development. I need to create a Add New sub-menu like this image:

WordPress dashboard sub-menu screenshot

My code is like below:

public function register_custom_post_type() {
    $supports = ['title'];
    $labels = [
        'name' => __( 'News', 'wpwi' ),
        'singular_name' => __( 'News', 'wpwi' ),
        'name_admin_bar' => __( 'News', 'wpwi' ),
        'add_new' => __( 'Add New', 'wpwi' ),
        'add_new_item' => __( 'Add New News', 'wpwi' ),
        'new_item' => __( 'New News', 'wpwi' ),
        'edit_item' => __( 'Edit News', 'wpwi' ),
        'all_items' => __( 'News', 'wpwi' ),
        'search_items' => __( 'Search News', 'wpwi' ),
        'not_found' => __( 'No News Found.', 'wpwi' ),
    ];

    $args = [
        'public' => true,
        'show_ui' => true,
        'supports' => $supports,
        'labels' => $labels,
        'description' => 'News Information',
        'has_archive' => true,
        'exclude_from_search' => false,
        'show_in_menu' => 'news_info',
    ];

    register_post_type( 'news_info', $args );
}

public function admin_menu() {
    add_menu_page( __( 'News Information', 'wpwi' ), 'News Info', 'manage_options', 'news_info', [$this, 'api_key_page'], 'dashicons-info-outline', 30 );
    add_submenu_page( 'news_info', __( 'News Information', 'wpwi' ), 'API Key', 'manage_options', 'news_info_page', [$this, 'api_key_page'], 0 );
    add_submenu_page( 'news_info', __( 'News Information', 'wpwi' ), 'Add News', 'manage_options', 'news_info', 'call_back_function', 1 );
}

I would like to show Admin Menu like below.

API Key
Add New
News Infos

1 Answer 1

2

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 
)

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

//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;
}
2
  • Thanks @Ali Alizadegan. How can I create a Add New menu ? I registered custom post type but I can't see Add New menu.
    – Foysal
    Jan 25, 2023 at 5:32
  • 1
    check your post type has these arguments : developer.wordpress.org/reference/functions/register_post_type and this: 'add_new' => __( 'Add New', 'textdomain' ), 'add_new_item' => __( 'Add New Book', 'textdomain' ), 'new_item' => __( 'New Book', 'textdomain' ), Jan 28, 2023 at 9:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.