Skip to content

Commit

Permalink
Lazy load iframes
Browse files Browse the repository at this point in the history
  • Loading branch information
LukeAbell committed Mar 27, 2024
1 parent 19c8fd2 commit b0baf3c
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/Helpers/EmbedHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ public static function renderEmbeds(string $html): string
{
return preg_replace_callback('/<!-- wp:embed.*?-->\s*?<figure class="wp-block-embed.*?".*?<div class="wp-block-embed__wrapper">\s*?(.*?)\s*?<\/div>.*?<\/figure>/', function (array $matches): string {
$embed = self::create($matches[1]);
$url = preg_replace('/\//', '\/', preg_quote($matches[1]));
$url = preg_replace('/\//', '\\/', preg_quote($matches[1]));

return preg_replace("/>\s*?{$url}\s*?</", ">{$embed->code?->html}<", $matches[0]);
// Lazy load iframes
$embedCodeWithLazyLoad = str_replace('<iframe', '<iframe loading="lazy"', $embed->code?->html);

return preg_replace("/>\s*?{$url}\s*?</", ">{$embedCodeWithLazyLoad}<", $matches[0]);
}, $html);
}

Expand Down

0 comments on commit b0baf3c

Please sign in to comment.