Add custom class to WordPress iframe video, youtube and vimeo iframe.

WordPress Iframe Video Filter

Using embed_oembed_html filter we will add custom class to wordpress iframe video, youtube and vimeo.

Usage Filter

Copy function code and paste it in your functions.php file.


function custom_class_to_wordpress_iframe_video( $html ) {

if( preg_match('/(youtube.com)/', $html) ){ // if youtube video
return str_replace('<iframe', '<iframe class="my-youtube-video"', $html); // add my custom class "my-youtube-video"
}

elseif( preg_match('/(vimeo.com)/', $html) ){ // if vimeo video
return str_replace('<iframe', '<iframe class="my-vimeo-video"', $html); // add my custom class "my-vimeo-video"
}

else{
return $html;
}

}
add_filter('embed_oembed_html', 'custom_class_to_wordpress_iframe_video', 99, 4);

Note: this is “my-youtube-video” youtube iframe class, change it to your custom class, and this is “my-vimeo-video” vimeo iframe class, change it to your custom class.

Do you want to add class to links? Use Extend Link Plugin, All-In-One.