Search Box for the "My Sites" Admin Bar Menu
Adds a filter field to the top of the toolbar's My Sites menu so super admins on large networks can type part of a name to find a site instantly.
Opens WPIDE on
Code
if ( ! is_multisite() ) {
return;
}
$wpide_min_sites = 10; // Show the filter above this many sites.
add_action( 'admin_bar_menu', function ( $bar ) use ( $wpide_min_sites ) {
if ( empty( $bar->user->blogs ) || count( $bar->user->blogs ) <= $wpide_min_sites ) {
return;
}
$bar->add_node( array(
'parent' => 'my-sites-list',
'id' => 'wpide-my-sites-filter',
'title' => '<label class="screen-reader-text" for="wpide-my-sites-q">' . esc_html__( 'Filter sites' ) . '</label><input type="search" id="wpide-my-sites-q" placeholder="' . esc_attr__( 'Filter sites…' ) . '" style="width:94%;margin:4px 3%;padding:2px 6px;height:26px;line-height:1;color:#1d2327">',
'meta' => array( 'html' => '' ),
) );
$filter = function () {
?>
<script>
document.addEventListener('input', function (e) {
if (e.target.id !== 'wpide-my-sites-q') { return; }
var q = e.target.value.toLowerCase();
document.querySelectorAll('#wp-admin-bar-my-sites-list > li').forEach(function (li) {
if (li.id === 'wp-admin-bar-wpide-my-sites-filter') { return; }
li.style.display = li.textContent.toLowerCase().indexOf(q) === -1 ? 'none' : '';
});
});
</script>
<?php
};
add_action( 'wp_footer', $filter, 99 );
add_action( 'admin_footer', $filter, 99 );
}, 19 ); // Before the list is built, so the field comes first.
Notes
Appears only when you belong to more than ten sites; change $wpide_min_sites to show it sooner.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.