0

I have two cases where I need to add a new admin menu that is based on current items. For example, I need to edit a menu page that will display pages with specific meta key so I added a new menu page like this:

add_menu_page( 'Landing Pages', 'Landing Pages', 'manage_options', 'edit.php?post_type=page&libray_type=landing_page' );

The issue is when I press the new menu item the pages menu is opening.

enter image description here

I have tried to use this filters with no success.

add_filter( 'parent_file', [ $this, 'admin_parent_file' ], 11 );
add_filter( 'submenu_file', [ $this, 'admin_submenu_file' ], 11, 2 );

function admin_parent_file( $parent_file ){
    if( $this->is_landing_page_query() ) {
        $parent_file = self::MENU_SLUG; // edit.php?post_type=page&libray_type=landing_page
    }

    return $parent_file;
}

I don't know if I'm doing something wrong but it looks like the filters are overwritten again somewhere.

Everything is in a class so don't worry about that.

0
0

I remembered a question I answered in March this year, so you may want to check it out here, but to highlight your "Landing Pages" menu item, you can do it like so:

Note: It will be up to you on how to implement the code in your class, but just let me know if you have any questions regarding the code.

  1. Add the custom menu item:

    function add_landing_pages_admin_menu() {
        $slug = 'edit.php?post_type=page&libray_type=landing_page';
    
        add_menu_page( 'Landing Pages', 'Landing Pages', 'manage_options', $slug );
    }
    add_action( 'admin_menu', 'add_landing_pages_admin_menu' );
    
  2. Change the "Pages" and "All Pages" menu items so that they do not get highlighted, but note that we're not changing the (global) $parent_file value here (refer to step #3 for that part).

    function fix_admin_parent_file_override( $menu ) {
        global $pagenow, $submenu, $parent_file;
    
        // If we're not on the edit.php page, do nothing.
        if ( ! is_admin() || 'edit.php' !== $pagenow ||
            'edit.php?post_type=page' !== $parent_file
        ) {
            return $menu;
        }
    
        // Same as in the above add_landing_pages_admin_menu().
        $slug = 'edit.php?post_type=page&libray_type=landing_page';
    
        // Make sure the $parent_file is not overriden (by WordPress) to the 'Pages' menu.
        if ( isset( $_GET['libray_type'] ) && 'landing_page' === $_GET['libray_type'] ) {
            // Add an empty libray_type query to the 'Pages' URL.
            $menu[20][2] .= '&libray_type=';
            $submenu[ $menu[20][2] ] = $submenu['edit.php?post_type=page'];
    
            // Then set the 'All Pages' URL to the same as the 'Pages' URL.
            $submenu[ $menu[20][2] ][5][2] = $menu[20][2];
    
            unset( $submenu['edit.php?post_type=page'] );
        }
    
        return $menu;
    }
    add_filter( 'add_menu_classes', 'fix_admin_parent_file_override' );
    
  3. Now change the $parent_file which then highlights the custom menu item added via step #1 above.

    function highlight_landing_pages_admin_menu( $parent_file ) {
        if ( isset( $_GET['libray_type'] ) && 'landing_page' === $_GET['libray_type'] &&
            'edit.php?post_type=page' === $parent_file
        ) {
            // Return the $slug value as in the above add_landing_pages_admin_menu()
            return 'edit.php?post_type=page&libray_type=landing_page';
        }
    
        return $parent_file;
    }
    add_filter( 'parent_file', 'highlight_landing_pages_admin_menu' );
    

BTW, that libray_type seems like a typo.. did you mean library_type?

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.