@@ -710,6 +710,12 @@ private static function get_responsive_container_markup( $attributes, $inner_blo
710710 $ tags = new WP_HTML_Tag_Processor ( $ overlay_blocks_html );
711711 $ overlay_blocks_html = block_core_navigation_add_directives_to_overlay_close ( $ tags );
712712 }
713+ // Images in the overlay are hidden until the menu is opened. Pre-set
714+ // fetchpriority="low" so that when wp_filter_content_tags() processes the
715+ // parent template part, it sees the attribute already present and calls
716+ // wp_get_loading_optimization_attributes() with fetchpriority="low", which both prevents
717+ // fetchpriority="high" from being added and stops the LCP counter from being incremented.
718+ $ overlay_blocks_html = block_core_navigation_set_overlay_image_fetch_priority ( $ overlay_blocks_html );
713719 }
714720
715721 $ has_custom_overlay = ! empty ( $ overlay_blocks_html );
@@ -1111,6 +1117,25 @@ function block_core_navigation_add_directives_to_overlay_close( $tags ) {
11111117 return $ tags ->get_updated_html ();
11121118}
11131119
1120+ /**
1121+ * Sets fetchpriority="low" on all IMG tags within the navigation overlay.
1122+ *
1123+ * Images in the overlay are hidden until the menu is opened, so they should
1124+ * not compete with any actual LCP element image on the page.
1125+ *
1126+ * @since 7.0.0
1127+ *
1128+ * @param string $overlay_blocks_html The rendered HTML of the overlay blocks.
1129+ * @return string Modified HTML with fetchpriority="low" on all IMG tags.
1130+ */
1131+ function block_core_navigation_set_overlay_image_fetch_priority ( string $ overlay_blocks_html ): string {
1132+ $ tags = new WP_HTML_Tag_Processor ( $ overlay_blocks_html );
1133+ while ( $ tags ->next_tag ( 'IMG ' ) ) {
1134+ $ tags ->set_attribute ( 'fetchpriority ' , 'low ' );
1135+ }
1136+ return $ tags ->get_updated_html ();
1137+ }
1138+
11141139/**
11151140 * Add Interactivity API directives to the navigation-submenu and page-list
11161141 * blocks markup using the Tag Processor.
0 commit comments