1

I have impelmented a custom code to wrap a video embed in a .embed-responsive class to make it responsive with CSS then:

PHP:

function kadabra_responsive_embed( $html ) {
    return '<div class="embed-responsive embed-responsive-16by9">' . $html . '</div>';
}
add_filter( 'embed_oembed_html', 'kadabra_responsive_embed', 10, 3 );
add_filter( 'video_embed_html', 'kadabra_responsive_embed' );

CSS:

.embed-responsive-16by9 {
  height: 0;
  padding-bottom: 56.25%;
}

which works pefect for a video embed. However, it also wraps other embeds, like a Twitter embed, which it is not required for. Any way to make it exclusive for video embeds only?

  • Just realized you mentioned shortcodes, but it sounds like you mean video oEmbeds in general, where you can also paste the url into the content editor. So I didn't have shortcodes specifically in mind in my answer – birgire Jul 25 '17 at 11:13
  • @birgire, thanks! I did actually mean video embeds via the Media button, so I think it is oEmbeds (sorry, I am not yet well versed in WP terms). I think the solution below should work! – Deka87 Jul 25 '17 at 11:20
1

You could create a new callback for the embed_oembed_html filter and target the third input argument, the oembed $url.

Then you could e.g. create boolean helper functions like (untested):

function is_oembed_from_video_specific_hosts_wpse274552( $url )
{
    return in_array(
        parse_url( $url, PHP_URL_HOST ),
        [
             'youtube.com',
             'youtu.be',
             'vimeo.com',  // ... etc
        ],
        true
    );
}

or a more detailed (more expensive) checks like (untested):

function is_video_oembed_wpse274552( $url )
{
    $video_providers = [
        '#https?://((m|www)\.)?youtube\.com/watch.*#i',
        '#https?://((m|www)\.)?youtube\.com/playlist.*#i',         
        '#https?://youtu\.be/.*#i',        
        '#https?://(.+\.)?vimeo\.com/.*#i',                        
        '#https?://(www\.)?dailymotion\.com/.*#i',                 
        '#https?://dai\.ly/.*#i',
        '#https?://videopress\.com/v/.*#',
        '#https?://wordpress\.tv/.*#i',
        '#https?://(www\.)?funnyordie\.com/videos/.*#i',
        '#https?://(www\.)?(animoto|video214)\.com/play/.*#i',
        '#https?://www\.facebook\.com/.*/videos/.*#i',
        '#https?://www\.facebook\.com/video\.php.*#i', // ... etc
    ];

    $is_video_oembed = false;
    foreach( $video_providers as $video_provider )
    {
        if ( preg_match( $video_provider , $url ) ) {
        {
             $is_video_oembed = true;
             break;
        }
    }
    return $is_video_oembed;
}

Note that oEmbed providers can be modified, added and deleted in the WordPress core and through filters. That could affect the above checks.

Hope you can adjust it to your needs!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.