0

I'm developing a very media-centered site with WordPress and am using the default categories and tags on uploaded media using, in part this plugin Media Categories.

I am displaying a sub-menu item under 'Media' for each 'Categories' and 'Tags' successfully using:

add_media_page( 'Tags', 'Tags', 'edit_posts' , 'edit-tags.php?taxonomy=post_tag');

It is linking through properly, but it seems somewhere in WordPress this page is assigned as a child of the default Posts type, as when I click it, it opens the 'Posts' menu and displays the Tags page, rather than keeping the 'Media' menu opened and displaying it from there. Is there any way to keep the parent page open (i.e. Media)?

This isn't an essential fix, but it would be good for the UI and continuity to have the Media menu stay open, rather than swapping to the Posts menu.

I can provide screenshots if this is confusing.

Thanks!

EDIT

I've just discovered a cheeky little definition in edit-tags.php: $parent_file = 'edit.php';

It seems each file corresponding to a submenu item in the WP Admin Menu has this $parent_file set. Now obviously, I can simply change this to 'upload.php' to refer to the Media parent, but I'm wondering if there's a non-hacky way that I can modify this value from functions.php in my theme file?

4 Answers 4

2

To add to what @brasofilo answered 3 years ago, this is what is necessary in the current menu system (WP v4.5.1).

add_action( 'admin_head-edit-tags.php', 'modify_menu_highlight_wpse_43839' );

function modify_menu_highlight_wpse_43839()
{
    if( 'post_tag' == $_GET['taxonomy'] )
    {       
        ?>
        <script type="text/javascript">
            jQuery(document).ready( function($) 
            {
                $("#menu-posts, #menu-posts a")
                    .removeClass('wp-has-current-submenu')
                    .removeClass('wp-menu-open')
                    .addClass('wp-not-current-submenu'); 
                $("#menu-media, #menu-media > a")
                    .addClass('wp-has-current-submenu');
            });     
        </script>
        <?php
    }
}

Hope this helps!

0

The sub_menu_page that you are trying to open is currently linked to the Tags sub_menu_page. Hopefully, someone will find a way of making it work. I am not sure if it able to display two menu pages with the same target such as in this case. Normally you might have to remove one or rename one.

add_action('admin_menu', 'm_video_disp');

function m_video_disp() {
    global $submenu;
    $submenu['edit.php'][16][0] = 'Videos';
    // add_media_page('Welcome to Videos', 'Videos', 'manage_options', 'edit-tags.php?taxonomy=post_tag', '');
    add_submenu_page('upload.php', 'Welcome to Videos', 'Videos', 'manage_options', 'edit-tags.php?taxonomy=post_tag', '');
}

add_submenu_page('upload.php', $other_args) and add_media_page($args) do the same thing in this case. I know this answer is insufficient, I hope someone can improve on it more.

1
  • Thanks, but using the global $submenu (or even $menu) still leaves me with the same issue that 'edit-tags.php?taxonomy=post_tag' is somehow linked to 'edit.php' in the menu. Even if I remove the default 'Posts' type from the menu, the submenu page will not open out as a child of 'Media'. Any ideas anyone?
    – lowe_22
    Feb 29, 2012 at 17:47
0

This has to be solved with jQuery. The following does it, but there are some secondary CSS effects that have to be dealt with.

menu tags moved to media

add_action( 'admin_head-edit-tags.php', 'modify_menu_highlight_wpse_43839' );

function modify_menu_highlight_wpse_43839()
{
    if( 'post_tag' == $_GET['taxonomy'] )
    {       
        ?>
        <script type="text/javascript">
            jQuery(document).ready( function($) 
            {
                $("#menu-posts").removeClass('wp-has-current-submenu'); 
                $("#menu-media").addClass('wp-has-current-submenu');
            });     
        </script>
        <?php
    }
}
0

There's a parent_file filter; something like this should work:

add_filter('parent_file', function($parent_file) {
    if (get_current_screen()->id === 'edit-post_tag') {
        return 'upload.php';
    }
    return $parent_file;
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.