How can I activate Collapse Menu in WordPress?

Inside the WordPress admin dashboard there is a button for making the left side menu collapse (to show just icons and not the icons + text on left side). How can I add jQuery so that it gets automatically collapsed once my plugin opens up. I tried this but it didn’t work for me. I want to give a sort of ‘full screen’ option inside dashboard. Can anyone help me on this?

I tried adding this jquery script to make it work, but it didn’t work for me:

<script>
jQuery(document).ready(function() {
     jQuery("#collapse-menu").click();
});

Related posts

Leave a Reply

2 comments

  1. You have to trigger the click event. Try,

    jQuery(document).ready(function() { jQuery("#collapse-menu").trigger('click'); });
    

    Update:
    Use the following code to check if menu is already folded, if not trigger click event to fold it.

    jQuery(document).ready(function() {
        if ( !$(document.body).hasClass('folded') ) {
            jQuery("#collapse-menu").trigger('click');
        }
    });
    
  2. None of that worked for me. I had to use jQuery(document.body).addClass('folded');
    Wordpress jQuery didnt like $(document.body).hasClass('folded'), I had to modify.
    Snippet below.

       jQuery(document).ready(function() {
        if ( !jQuery(document.body).hasClass('folded') ) {
            jQuery(document.body).addClass('folded');
        }
        });