post-thumb-admin-menu

UX – the WordPress admin menu

Adding list subitems Grouping of custom post types can be achieved easily. If you made use of the

'show_ui' argument and set it to true, then you have access to more advanced functionality: The 'show_in_menu' argument. It can be set to boolean true/false or to a string. If you make use of the string, then you can add sub menu items to main menu items. In our example we could add Location to Event, using 'edit.php?post_type=event'.

// If our key/index is 0, then we got the main menu item and
// if not, we got a sub menu item and use the main slug as parent
$show_in_menu = 0 !== $index
    ? 'edit.php?post_type='.strtolower( $post_types[0] ) : true;

register_post_type( strtolower( $post_type ), array(
     'public'       => true
    ,'show_ui'      => true
    ,'show_in_menu'     => $show_in_menu
    ,'menu_position'    => 12
    ,'labels'       => $labels
) );

It’s basically the same as using

remove/add_submenu_page();, but a little faster and more integrated. And again: We ran into a problem. You can’t specify at which positions the sub menu items will appear. You can only specify the main menu position when using add_menu_page() or when registering the main custom post type. But that’s not the only problem.

Adding “Add new” subitems We’re missing our “Add New Event/Location” links. Both pages are still accessible (remember the tiny grey button on top of the post type list screens?), but they are not present in the menu. That can be fixed easily with using

add_submenu_page();. Just use 'edit.php?post_type=party' as $parent_slug and 'post-new.php?post_type=location' as $menu_slug. Then you’re ready to go and view your new menu.

/**
 * Add the "Add New [CPT]" sub menu item to the CPT main menu item
 * @return void
 */
function oxo_add_cpt_submenu_pages()
{
    foreach ( array( 'promoter', 'location' ) as $post_type )
    {
        // get the custom post type from the global
        $post_type = $GLOBALS['wp_post_types'][ $post_type ];
        add_submenu_page(
             'edit.php?post_type=event'
            ,$post_type->labels->new_item
            ,$post_type->labels->add_new_item
            ,"publish_{$post_type->capability_type}s"
            ,"post-new.php?post_type={$post_type->query_var}"
        );
    }
}
add_action( 'admin_menu', 'oxo_add_cpt_submenu_pages' );

2 comments:

  1. Pingback: Anety

Comments are closed.