0

Can anyone tell me how to stop the top level menu items in the WordPress Admin sidebar auto-expanding when that top level item is clicked, or a submenu item chosen from the fly-out menu?

For clarity, I'm not talking about the left/right expansion/collapse of the sidebar menu itself, but the up/down expansion to show submenu level items. e.g. The sidebar shows

Posts : Pages : Appearance : Tools : Settings : ... etc.

If I hover over "Appearance", the submenu items are presented as a fly-out, allowing me to click on one. However, when I do, or if I click on "Appearance" itself, that item auto-expands so that the sidebar now shows

Posts : Pages : Appearance - Themes - Widgets - Background : Tools : Settings : ... etc.

I want it to not expand on selection of a top level or sub menu item. I've searched Google and specialist sites like this one several times for an answer but can't find one. Plenty about expanding / collapsing the sidebar itself but only one about top level / submenu expansion. The proposed solution there (call to a javascript function) did not work for me.

Although it's trivial in the scheme of things, being unable to find the answer is starting to really frustrate me every time it does it!

Many thanks in anticipation.

  • Are you talking about modifying the WordPress admin, or something in your theme? – Jacob Peattie Jan 24 at 7:06
  • Sorry for not being clearer. The WordPress Admin sidebar. – Kevin4fm Jan 24 at 7:07
0

You can use this code

add_action('adminmenu', function() {
   ?>
    <script>
    jQuery('#adminmenu .wp-has-current-submenu.wp-menu-open')
      .removeClass('wp-has-current-submenu')
      .removeClass('wp-menu-open')
      .addClass('wp-not-current-submenu');
    </script>
    <?php
});
| improve this answer | |
  • Thanks Vitauts but this isn't working for me. It may be the same, or similar, Javascript solution I mentioned in the question. I've tried adding the function via an "Add Functions" plugin, and putting it directly into the site's current theme's <i>function.php</i>. None of these work. Should I be putting it somewhere else. – Kevin4fm Jan 24 at 12:14

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.