From a43a096883b39112589054f49365efa7e787ed0e Mon Sep 17 00:00:00 2001 From: Jb Audras Date: Thu, 7 Apr 2022 22:12:34 +0000 Subject: [PATCH] Toolbar: Add a filter to help remove site icons from toolbar for large multisite, and lazy load them by default. This changeset introduces the `wp_admin_bar_show_site_icons` filter to help developers to hide site icons from the toolbar, as it may have negative performance impact on large multisites. It also adds a default lazy load behavior for these icons. Props wslyhbb, sabernhardt, lkraav, kebbet, peterwilsoncc. Fixes #54447 git-svn-id: https://develop.svn.wordpress.org/trunk@53100 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/admin-bar.php | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/admin-bar.php b/src/wp-includes/admin-bar.php index 6f05fb7b26..274212acf7 100644 --- a/src/wp-includes/admin-bar.php +++ b/src/wp-includes/admin-bar.php @@ -611,14 +611,27 @@ function wp_admin_bar_my_sites_menu( $wp_admin_bar ) { ) ); + /** + * Filters whether to show the site icons in toolbar. + * + * Returning false to this hook is the recommended way to hide site icons in the toolbar. + * A truthy return may have negative performance impact on large multisites. + * + * @since 6.0.0 + * + * @param bool $show_site_icons Whether site icons should be shown in the toolbar. Default true. + */ + $show_site_icons = apply_filters( 'wp_admin_bar_show_site_icons', true ); + foreach ( (array) $wp_admin_bar->user->blogs as $blog ) { switch_to_blog( $blog->userblog_id ); - if ( has_site_icon() ) { + if ( true === $show_site_icons && has_site_icon() ) { $blavatar = sprintf( - '', + '', esc_url( get_site_icon_url( 16 ) ), - esc_url( get_site_icon_url( 32 ) ) + esc_url( get_site_icon_url( 32 ) ), + ( wp_lazy_loading_enabled( 'img', 'site_icon_in_toolbar' ) ? ' loading="lazy"' : '' ) ); } else { $blavatar = '
';