How to Add a Sub Menu Page to a Custom Post Type?

I am trying to create a sub-menu under a Custom Post Type I have named Portfolios.

When I change add_submenu_page() to add_options_page(), it correctly shows a new link under the Settings menu, but it doesn’t show under the Portfolios menu.

Read More

What am I doing wrong?

Below is my code snippet;

add_action( 'admin_menu', 'mt_add_pages' );

function mt_add_pages() {
    add_submenu_page(
        __( 'portfolios', 'menu-test' ),
        __( 'Test Settings', 'menu-test' ),
        'manage_options',
        'testsettings',
        'mt_settings_page'
    );

    function mt_settings_page() {
        echo "<h2>" . __( 'Test Settings', 'menu-test' ) . "</h2>";
    }
}

Related posts

Leave a Reply

4 comments

  1. add_options_page() automatically adds it underneath settings, however add_submenu_page() gives you control as to where you want it to show up.

    Try something like this:

    add_submenu_page(
        'edit.php?post_type=portfolios',
        __( 'Test Settings', 'menu-test' ),
        __( 'Test Settings', 'menu-test' ),
        'manage_options',
        'testsettings',
        'mt_settings_page'
    );
    
  2. To expand on @Jai example…

    My Settings

    $postType = 'foo';
    $categoryType = 'bar';
    

    Custom Post Type

                $args = array(
                        'labels'             => array('name'=>$postType, ...),
                        'rewrite'            => array('slug' => 'all-'.$postType),
                        'taxonomies'         => array($categoryType)
                );
    
    register_post_type( 'foo', $args );
    

    Custom Category Taxonomy

                $args = array(
                        'labels'            => array( 'name' => _x( $categoryType, 'taxonomy general name' )),
                        'rewrite'           => array( 'slug' => $categoryType ),
                );
    
    register_taxonomy( $categoryType, array( $postType ), $args );
    

    Add Categories as submenu items

        $wp_term = get_categories( 'taxonomy='.$categoryType.'&type='.$postType ); 
        if ( $wp_term ) {
            foreach ( $wp_term as $term ) {
                // add_submenu_page( string $parent_slug, string $page_title, string $menu_title, string $capability, string $menu_slug,                                                  callable $function = '' )
                add_submenu_page(    'edit.php?post_type='.$postType,      $term->name,        $term->name,        'manage_options',   'edit.php?post_type='.$postType.'&'.$categoryType.'='.$term->slug, ''); 
            }
        } 
    
  3. add_submenu_page('edit.php?post_type='.$this->plugin->posttype, __('Settings', $this->plugin->name), __('Settings', $this->plugin->name), 'manage_options', $this->plugin->name, array(&$this, 'adminPanel'));
    

    there is admin panel is a callback function name.

  4. /**
    * Adds a submenu page under a custom post type parent.
    */
    function books_register_ref_page() {
        add_submenu_page(
            'edit.php?post_type=book',
            __( 'Books Shortcode Reference', 'textdomain' ),
            __( 'Shortcode Reference', 'textdomain' ),
            'manage_options',
            'books-shortcode-ref',
            'books_ref_page_callback'
        );
    }
    
    /**
    * Display callback for the submenu page.
    */
    function books_ref_page_callback() { 
        ?>
        <div class="wrap">
            <h1><?php _e( 'Books Shortcode Reference', 'textdomain' ); ?></h1>
            <p><?php _e( 'Helpful stuff here', 'textdomain' ); ?></p>
        </div>
        <?php
    }
    

    Link to Source , Author: Christina Blust