Halloween Sale 15% off Ends in See the plans
PHP

Search Box for the Admin Menu

Adds a small filter box at the top of the admin menu that hides every menu and submenu item not matching what you type, handy on sites with dozens of plugin menus.

Don’t have WPIDE yet? Install it on your site

Code

add_action( 'admin_footer', function () {
    ?>
    <style>
    #wpide-menu-filter{margin:8px 10px;width:calc(100% - 20px);box-sizing:border-box;min-height:28px}
    .folded #wpide-menu-filter{display:none}
    #adminmenu li.wpide-hide{display:none}
    #adminmenu.wpide-filtering .wp-submenu{position:static!important;display:block!important;box-shadow:none!important;margin:0!important}
    </style>
    <script>
    ( function () {
        var menu = document.getElementById( 'adminmenu' );
        if ( ! menu ) { return; }
        var input = document.createElement( 'input' );
        input.type = 'search';
        input.id = 'wpide-menu-filter';
        input.placeholder = 'Filter menu...';
        input.setAttribute( 'aria-label', 'Filter admin menu' );
        menu.parentNode.insertBefore( input, menu );

        input.addEventListener( 'input', function () {
            var term = input.value.trim().toLowerCase();
            menu.classList.toggle( 'wpide-filtering', term !== '' );
            menu.querySelectorAll( ':scope > li.menu-top' ).forEach( function ( item ) {
                var top = ( item.querySelector( '.wp-menu-name' ) || item ).textContent.toLowerCase();
                var subs = item.querySelectorAll( '.wp-submenu li:not(.wp-submenu-head)' );
                var anySub = false;
                subs.forEach( function ( sub ) {
                    var match = term === '' || top.indexOf( term ) !== -1 || sub.textContent.toLowerCase().indexOf( term ) !== -1;
                    sub.classList.toggle( 'wpide-hide', ! match );
                    anySub = anySub || ( match && term !== '' );
                } );
                item.classList.toggle( 'wpide-hide', term !== '' && top.indexOf( term ) === -1 && ! anySub );
            } );
            menu.querySelectorAll( ':scope > li.wp-menu-separator' ).forEach( function ( sep ) { sep.classList.toggle( 'wpide-hide', term !== '' ); } );
        } );
        input.addEventListener( 'keydown', function ( event ) {
            if ( event.key === 'Escape' ) { input.value = ''; input.dispatchEvent( new Event( 'input' ) ); }
        } );
    } )();
    </script>
    <?php
} );

Notes

Press Escape to clear the filter; the box is hidden while the menu is collapsed.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.