How to Add Custom Class in Iframe When Embeding Youtube Video

When wordpress deal with Youtube, you can easily to add a youtube video, just paste youtube url video and wordpress will working to show the video in wordpress editor. This is feature on wordpress 4.0, everyone are waiting this feature come true. And right now, you can try it.

The problem is, when you want to add custom class to iframe or embeded code automaticly, how to do that?

I just search the solution in the internet and i was found best solution for it.  Just add this code to your functions.php file

add_filter( 'embed_oembed_html', 'custom_youtube_oembed' );
function custom_youtube_oembed( $code ){
    if( stripos( $code, '' ) !== FALSE && stripos( $code, 'iframe' ) !== FALSE )
        $code = str_replace( '<iframe', '<iframe class="add your custom class here"  ', $code );

    return $code;

Its code will wordpress ability to detect it is a youtube link  or not, if yes, then add custom class. Just replace add your custom class here  with your class. Done, just try it.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.