Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save sabrysuleiman/4eb9490e9c0aa396d6e037be60916d2a to your computer and use it in GitHub Desktop.

Select an option

Save sabrysuleiman/4eb9490e9c0aa396d6e037be60916d2a to your computer and use it in GitHub Desktop.
Fix Youtube Iframe ( Error 153 ) for Wordpress Content
function fix_youtube_153_and_lazy_load($content) {
if (is_admin() || empty($content)) {
return $content;
}
// This handles both images and iframes
$content = preg_replace_callback(
'/<(img|iframe)([^>]+)>/i',
function ($matches) {
$tag = $matches[1];
$attributes = $matches[2];
// 1. Add lazy loading if not already there
if (strpos($attributes, 'loading=') === false) {
$attributes .= ' loading="lazy"';
}
// 2. Add Referrer Policy (Fixes Error 153 for YouTube)
if ($tag === 'iframe' && strpos($attributes, 'referrerpolicy=') === false) {
$attributes .= ' referrerpolicy="strict-origin-when-cross-origin"';
}
return '<' . $tag . $attributes . '>';
},
$content
);
return $content;
}
add_filter('the_content', 'fix_youtube_153_and_lazy_load', 99);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment