Wrong current class on admin menu with add_submenu_page()

I’m writing a plugin that displays a post/page list in the admin menu, as a submenu for each post type. Here’s the code I’ve got far: http://pastebin.com/DmUGfzxN

The lists are showing and linking fine. However, I have a problem when I go to another menu, for example, Media, and go to edit a post (or image in this case). The current class that WordPress uses for the admin menu points to the wrong place, and opens the menu for Pages, highlighting the last submenu item.

Read More

I believe this is a related problem here: Current class on admin menu using add_submenu_page()

He solved it by adding the submenu page to a page slug, instead of a link like admin.php?=page.... Unfortunately, I want my submenus to be under Post, Page, etc., so I need to use edit.php and edit.php?post_type=....

If anyone has any ideas how to get around this quirk/bug, I’d appreciate it. Thank you.

Related posts

1 comment

  1. I rewrote the code:

    /*
    Plugin Name: Admin Menu Post List
    Plugin URI:
    Description: Display a simple post list in admin menu for easy access
    Version: 0.2
    Author: Eliot Akira
    Author URI:
    License: GPL2
    */
    
    
    /*
     * Load CSS in header
     */
    
    function custom_post_list_view_css() { ?>
    
            <style>
                    .post_list_view_headline {
                            padding-left: 10px !important;
                            padding-right: 10px !important;
                    }
                    .post_list_view_post {
                            margin-left:12px;
                    }
                    .post_current a {
                            color:white !important;
                    }
        </style>
    
    <?php }
    add_action( 'admin_head', 'custom_post_list_view_css' );
    
    
    /*
     * Admin Menu Post List
     */
    
    
    add_action('admin_menu', 'custom_post_list_view');
    function custom_post_list_view() {
    
            /*** Get options ***/
    
            $post_types = array( 'post', 'page' );
    
            $current_post_ID = $_GET['post']; /* Get current post ID on admin screen */
    
            foreach ($post_types as $post_type) {
    
                    $custom_menu_slug = $post_type;
                    $output = '';
    
                    $args = array(
                            "post_type" => $post_type,
                            "parent" => "0",
                            "post_parent" => "0",
                            "numberposts" => "-1",
                            "orderby" => "menu_order",
                            "order" => "ASC",
                            "post_status" => "any",
                            "suppress_filters" => 0
                    );
    
                    $posts = get_posts($args);
    
                    if($posts) {
    
                            $output .= '</a>';
    
                            $output .= '<div class="list_view_' . $post_type . '">'
                                                    . '<div class="post_list_view_headline">' . '<hr>' . '</div>';
                            foreach ($posts as $post) {
                                    $edit_link = get_edit_post_link($post->ID);
                                    $title = get_the_title($post->ID);
                                    $title = esc_html($title);
                                    $output .= '<div class="post_list_view_post';
                                    if($current_post_ID == ($post->ID)) {
                                            $output .= ' post_current';
                                    }
                                    $output .= '">'
                                            . '<a href="'
                                            . $edit_link    . '">'
                                            . $title . '</a></div>';
    
                                    /*** Search for children? ***/
    
                            }
                            $output .= '</div>';
    
                            $output .= '<a>';
    
                            if($post_type == 'post') {
                                    add_posts_page( "Title", $output, "edit_posts", $custom_menu_slug, "custom_post_list_view_page");
                            } else {
                                     if ($post_type == 'page') {
                                            add_pages_page( "Title", $output, "edit_pages", $custom_menu_slug, "custom_post_list_view_page");
                                    } else {
                                            if($post_type == 'attachment') {
                                                     add_media_page("Title", $output, "edit_posts", $custom_menu_slug, "custom_post_list_view_page");
                                            } else {
                                                    add_submenu_page(('edit.php?post_type=' . $post_type), "Title", $output, "edit_posts", ('edit.php?post_type=' . $post_type), "custom_post_list_view_page");
                                            }
                                    }
                            }
                    }
            } // End foreach post type
    }
    
    function custom_post_list_view_page() { /* Empty */ }
    

    Now it displays the submenu, and the current class doesn’t affect it. Where I output the submenu, I changed the <ul> and <li> to <div>. I added a check to see if the menu item equals the current post being edited, and a little CSS in the admin_head to highlight the item. Please refer to the code paste above.

    Next, I’ll be looking into displaying child pages.

Comments are closed.