21

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.

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>";
    }
}
1
  • i think you are passing incorrent parent slug, i.e portfolios. check it again... inplace of portfolios pass the slug of your custom post type.. Aug 23, 2013 at 11:38

4 Answers 4

41

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'
);
1
  • 1
    Your code won't work unless, you're missing an third argument of menu_title. See the codex Nov 2, 2013 at 11:37
5

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, ''); 
        }
    } 
4
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.

1
  • 3
    Can you explain a little bit more?
    – bravokeyl
    Jan 3, 2017 at 6:05
3
/**
* 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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