Embed block: add lazy-load iframe option#77604
Embed block: add lazy-load iframe option#77604sidharthpandita1 wants to merge 1 commit intoWordPress:trunkfrom
Conversation
Adds an optional "Lazy-load" toggle to the Embed block's Media settings panel in the inspector. When enabled, the server-side render callback injects loading="lazy" onto the block's iframe, deferring offscreen video loads without affecting existing embeds (default: false). - block.json: add `lazyLoad` boolean attribute (default false) - embed-controls.js: add Lazy-load ToggleControl to Media settings panel - edit.js: wire lazyLoad attribute and toggleLazyLoad handler - index.php: PHP render_callback using WP_HTML_Tag_Processor Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
|
Thanks for the PR, but did you know that WordPress, by default, adds the loading attribute to img and iframe elements based on specific rules? This is achieved through functions such as Therefore, we do not recommend adding specific functionalities to specific blocks. It is already possible to control how the loading attribute is applied through various filters. |
What?
Closes <!#77594>
Adds an optional Lazy-load toggle to the Embed block's Media settings inspector panel. When enabled, the block's iframe is rendered with
loading="lazy", deferring the load of offscreen embeds (e.g. YouTube videos below the fold). The feature is opt-in and off by default, so all existing embeds are unaffected.Why?
Pages with multiple video embeds — especially YouTube — can suffer significant performance penalties because all iframes load eagerly on page load, regardless of whether they are visible. Native
loading="lazy"on iframes is well-supported and recommended by modern web performance guidelines (Core Web Vitals, Lighthouse). Currently there is no built-in way to enable this in the Embed block without custom code or a third-party plugin.How?
block.json— Registers a newlazyLoadboolean attribute (defaultfalse).embed-controls.js— Adds aLazy-loadToggleControlinside the existingMedia settingsToolsPanelin the block inspector. The panel now renders for all embeds (not only responsive ones) so the toggle is always accessible.edit.js— DestructureslazyLoadfrom attributes, definestoggleLazyLoad(), and passes both down toEmbedControls.index.php— Introduces a PHPrender_callback(render_block_core_embed) that usesWP_HTML_Tag_Processorto injectloading="lazy"on every<iframe>in the rendered block HTML whenlazyLoadistrue. Whenfalse, content is returned unchanged.No changes to
save.js— the attribute is stored only in the block comment delimiter and applied entirely server-side, keeping the feature backward-compatible.Testing Instructions
https://www.youtube.com/watch?v=dQw4w9WgXcQ) and click Embed.<iframe>element — it should haveloading="lazy".loading="lazy"should be absent.lazyLoadattribute saved) render withoutloading="lazy"(backward compat).Testing Instructions for Keyboard
Tab/ arrow keys.Ctrl+Shift+,(or via the toolbar options menu).Tabthrough the Media settings panel to reach the Lazy-load toggle.Spaceto toggle it on/off — confirm the state changes correctly.Ctrl+Sand verify the result on the frontend as above.Screenshots or screencast
Before | After -- | -- Media settings panel — only "Resize for smaller devices" shown (when responsive theme) | Media settings panel — "Lazy-load" toggle visible for all embed types