I’m having a problem here:
I am passing this through a custom field: http://www.youtube.com/watch?v=E6P1Q-UycHA&autoplay=1
(notice the autoplay=1
)
But when I load the video on my theme using wp_oembed_get… it displays the video fine, but it does not listen to the autoplay=1
variable I am passing through.
I need the videos to play on the load of the page.
Any ideas?
Thanks,
Alain Fontaine
Those are not really arguments like for YouTube, more of arguments for WordPress itself.
One way to handle it would be to access your argument later inside of a filter and modify HTML output.
Pass in arguments array:
And filter:
I know this question is pretty old, but I have a working solution that I figure might be helpful to anyone else trying to find this info and coming up empty in Google searches.
This solution has been tested in WordPress 3.7.1+:
Add the above in your theme’s
functions.php
to cause all YouTube videos added via oEmbed to autoplay.How This Works
In class-oembed.php on line 212, the
fetch()
method applies the filteroembed_fetch_url
to allow modifications to the URL.To avoid adding useless parameters to other oEmbed providers, we check for “youtube” in the provider URL â we could also check for “vimeo” and/or other video services â and, if that string is present, adds an
autoplay
argument to the query string.This is my solution in functions.php
enjoy
Cross-posting my answer from https://stackoverflow.com/a/55053642/799327, hope that’s okay since it’s from the same OP:
So, after some research on this, the best way to do this is to leverage the
oembed_fetch_url
filter hook to add extra arguments to the oEmbed request URL. My specific goal was to allow an autoplay paramater, but this method is built to be easy to tailor to any oEmbed argument you need.First, add this to your
functions.php
:This function takes the generated oEmbed URL and its corresponding arguments and checks it agains a hard-coded list of whitelisted arguments, in this case
['autoplay']
. If it sees any of these whitelisted keywords in the arguments passed to the oEmbed filter, it adds them with their given value to the oEmbed URL.Then, all you need to do is add the oEmbed parameter to your shortcode in the WordPress editor, like this:
Be aware that the oEmbed class in WP uses the postmeta as a cache for these requests, so if you’ve embedded the target URL before, you might have to clear your postmeta cache in some way or add a cache buster of some kind to the target URL. If the link is in the cache, the filter hook will never get to run.
I hope this makes sense, as I feel like it’s a pretty useful feature that’s surprisingly hard to figure out how to achieve.
Anything after the first parameter must be sent as an array of arguments.
See the example given on the codex page.
http://codex.wordpress.org/Function_Reference/wp_oembed_get
If you’re attempting to take a custom field value and have the embed code convert it into a video(ie. what would happen if you placed it into the content), then you can do something like this…
This of course will run several filters over the data that you don’t need, but it does the trick all the same..
After all research what i found is this. below is the code.
Credits to https://gist.github.com/mustardBees/7704269#file-functions-php.
Thanks!
I don’t know if it’s needed any more, but I’ll post the answer here
also, as Gist: https://gist.github.com/zachakbar/3100e093cb8484d47586a259f9f960dc
This can be easily fixed by modifying the wp_oembed_get() function in wp-includes/media.php to this: