Created
January 13, 2026 12:50
-
-
Save sabrysuleiman/4eb9490e9c0aa396d6e037be60916d2a to your computer and use it in GitHub Desktop.
Fix Youtube Iframe ( Error 153 ) for Wordpress Content
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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