add_filter to youtube embeds?

The question:

hey guys,
when posting a youtube video link in the backend (in a post or a page) wordpress is automatically creating the embed code for me.

Is it possible to add a filter to that?
I’d love to change the width and height of all embedded videos to 100%?

e.g.

<object width="100%" height="100%">
<param name="movie" value="http://www.youtube.com/v/rBa5qp9sUOY?version=3">
<param name="allowFullScreen" value="true">
<param name="allowscriptaccess" value="always">
<embed src="https://www.youtube.com/v/rBa5qp9sUOY?version=3" type="application/x-shockwave-flash" width="100%" height="100%" allowscriptaccess="always" allowfullscreen="true">
</object>`

Any idea how to solve this?

edit: Or is it at least possible to add a classname to the object tag so I can use javascript to influence with and height of the embedded video?

update: Thank you I tried the following piece of code but it doesn’t work?

add_filter('oembed_result','oembed_result', 10, 3);

function oembed_result($html, $url, $args) {

    // $args includes custom argument
    // modify $html as you need

    //return $html; 
}

if return $html is a comment no youtube video should appear right, however it does!

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

Yes, there is a filter for Oembeds. Two (or even more) in fact: oembed_result will be called before it is put in the cache (so only once per external embed), and embed_oembed_html after the cache (so every time the item is displayed). If you only need to modify it once, oembed_result is probably your friend. The second parameter is the $url, so check whether it comes from Youtube before you do something.


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 Comment