Skip to content

Instantly share code, notes, and snippets.

@stompweb
Last active August 29, 2015 14:00
Show Gist options
  • Save stompweb/11321145 to your computer and use it in GitHub Desktop.
Save stompweb/11321145 to your computer and use it in GitHub Desktop.
Change menu classes
<?php
function register_my_page() {
global $my_page;
$my_page = add_menu_page( 'My Page', 'My Page', 'edit_pages', 'my_page.php', 'my_page_hook', 'dashicons-groups', 4 );
}
add_action('admin_menu', 'register_my_page');
function highlight_different_menu($parent_file){
global $current_screen;
global $my_page;
if ( $current_screen['base'] == $my_page ) {
$parent_file = 'my_other_page.php';
}
return $parent_file;
}
add_filter('parent_file', 'highlight_different_menu');
@wpmark
Copy link

wpmark commented May 1, 2014

Done some digging here and the problem lies when you want to set the parent file to a menu item you have custom added. It seems because the $pagenow global only store edit-tags.php instead of edit-tag.php?taxonomy=category.

Therefore the page I want to make it a parent of is admin.php?page=wpmark_content which then doesn't work. The code I am using is below:

function wpmark_menu_hierarchy_correction( $parent_file ) {

    global $current_screen;
    global $pagenow;

    $wpmark_pagenow = $pagenow;

    switch( $wpmark_pagenow ) {

        /* if this is the categories admin screen */
        case 'edit-tags.php':
            $parent_file = 'admin.php?page=wpmark_content';
            break;

        default:
            $parent_file = $parent_file;

    }

    /* return the new parent file */
    return $parent_file;

}

add_filter( 'parent_file', 'wpmark_menu_hierarchy_correction' );

@stompweb
Copy link
Author

stompweb commented May 1, 2014

Can you show me how you've added the custom page? add_menu_page()? add_submenu_page()?

@wpmark
Copy link

wpmark commented May 1, 2014

Yes sure.

function wpmark_add_top_level_admin_menus() {

    /* if the user is a super admin */
    if( is_super_admin() )
        return;

    $wpmark_top_level_admin_menus = apply_filters( 'wpmark_top_level_admin_menus',
        array(

            /* this is the contnet menu item */
            array(
                'page_title' => 'Add Content',
                'menu_name' => 'Content',
                'capability' => 'edit_pages',
                'menu_slug' => 'wpmark_content',
                'content_function' => 'wpmark_content',
                'icon_location' => 'div',
                'menu_position' => 2,
            )

            /* other arrays in here doing other top level pages */

        )

    );

    /* loop through each menu item to add */
    foreach( $wpmark_top_level_admin_menus as $wpmark_top_level_admin_menu ) {

        /* add the menu page */
        add_menu_page(
            $wpmark_top_level_admin_menu[ 'page_title' ],
            $wpmark_top_level_admin_menu[ 'menu_name' ],
            $wpmark_top_level_admin_menu[ 'capability' ],
            $wpmark_top_level_admin_menu[ 'menu_slug' ],
            $wpmark_top_level_admin_menu[ 'content_function' ],
            $wpmark_top_level_admin_menu[ 'icon_location' ],
            $wpmark_top_level_admin_menu[ 'menu_position' ]
        );

    } // end foreach loop

}

add_action( 'admin_menu', 'wpmark_add_top_level_admin_menus' );

@stompweb
Copy link
Author

stompweb commented May 1, 2014

Ok, so I've changed the original gist. When I register a menu item I store the hook that's returned in a global. I then compared that to the base of the current screen to set a new class.

@wpmark
Copy link

wpmark commented May 1, 2014

Still doesn't solve the fact that $parent_file expects admin.php not admin.php?page=wpmark_content. In your example above you are setting the parent file to an already existing WordPress admin top level menu.

@stompweb
Copy link
Author

stompweb commented May 1, 2014

You should be able to just pass wpmark_content then (i.e. your menu slug), I was just giving an example in mine.

@stompweb
Copy link
Author

stompweb commented May 1, 2014

I always tend to add .php as per the codex example too. So wpmark_content.php

@wpmark
Copy link

wpmark commented May 2, 2014

Unfortunately that does not work :-(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment