How to wrap oEmbed-embedded video in DIV tags inside the_content?

All we need is an easy explanation of the problem, so here it is.

I am making a WordPress Theme for a website with video tutorials. I would like to put the video that is embedded in the content (with oEmbed) in a apart div.

An example

The full content (output from the_content()) is something like this:

<p><iframe src="https://player.vimeo.com/video/0000000" width="900" height="506" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe></p>
<p>This is an Test of a tutorial. Bla bla bla</p>

And I would like to get this to:

<div id="video">
<iframe src="https://player.vimeo.com/video/0000000" width="900" height="506" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>
</div>
<div id="content">
<p>This is an Test of a tutorial. Bla bla bla</p>
</div>

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

The embed_oembed_html filter runs before an oEmbed resource’s HTML is outputted, so you could hook into this and wrap the output in a div as below. I can’t think of a simple way of wrapping the other content.

add_filter('embed_oembed_html', 'my_embed_oembed_html', 99, 4);
function my_embed_oembed_html($html, $url, $attr, $post_id) {
  return '<div id="video">' . $html . '</div>';
}

Method 2

If you are attempting to use oEmbed in your WordPress theme template, try this:

<aside>
    <p>oEmbed video in template test</p>
    <?php echo apply_filters('the_content', "http://vimeo.com/41205967"); ?>
</aside>

This snippet will display a video from Vimeo.com directly in your theme, without having to create a post manually.

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply