1

I'm coding a new Wordpress plugin that registers several new post types. They all have their own menu and submenus in the Wordpress backend. How could create a new menu (like a parent category) and move all the stuff under it ?

0

Set the argument

    'show_in_menu'  => false

while registering your post with register_post_type() would make admin menus for that post type ignored, so this is the best solution here to hide menus.

If you can't access it, you could hook a function on admin_menu to add or remove menus and submenus.

Here's an example. We did create an 'artist' post type, but we don't want an artist admin menu. We would like to put it under a 'music' menu; that would have various other submenus.

    add_action( 'admin_menu', 'adjust_admin_menu' );

    function adjust_admin_menu(){

        $menu_slug = 'music'; //menu slug (or path to file); as ID
        $post_type_artist_slug = 'artist';
        $post_type_artist = get_post_type_object($post_type_artist_slug);

        /////Delete the menu generated by register_post_type() for our custom post type 'artist'.  When registering a post type, setting 

        $remove_menu_slug = sprintf('edit.php?post_type=%s',$post_type_artist_slug); //menu slug (here, a path to file); as ID

        //remove the menu
        remove_menu_page( $remove_menu_slug );
        /*
        //OR remove the 'add new' submenu
        remove_submenu_page( 
             $remove_menu_slug,
            sprintf('post-new.php?post_type=%s',$post_type_artist_slug) //SUBmenu slug (here, a path to file); as ID
        );
        */
        /////Create our custom menu

        $this->menu_page = add_menu_page( 
            __( 'Music', 'music-plugin' ), //page title - I never understood why this parameter is needed for.  Put what you like ?
            __( 'Music', 'music-plugin' ), //menu title
            'manage_options', //cappability
            $menu_slug,
            array($this,'settings_page'), //this function will output the content of the 'Music' page.
            'dashicons-album', // an image would be 'plugins_url( 'myplugin/images/icon.png' )'; but for core icons, see https://developer.wordpress.org/resource/dashicons 
            6
        ); 

        ////Add submenus

         add_submenu_page(
                $menu_slug,
                $post_type_artist->labels->name, //page title - I never understood why this parameter is needed for.  Put what you like ?
                $post_type_artist->labels->name, //submenu title
                'edit_posts',
                sprintf('edit.php?post_type=%s',$post_type_artist_slug) //SUBmenu slug (here, a path to a file); as ID

         );

         add_submenu_page(
                $menu_slug,
                $post_type_artist->labels->add_new_item,
                $post_type_artist->labels->add_new_item,
                'edit_posts',
                sprintf('post-new.php?post_type=%s',$post_type_artist_slug) //SUBmenu slug (here, a path to a file); as ID

         );
    }
| improve this answer | |

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.