I’m having this problem.
I am passing this through a custom field here
(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.
I think the way to do it is using wordpress filters:
This is my solution in functions.php
Enjoy!
look up function wp_oembed_get and use the args to pass the autoplay… should work fine. Just paste in the url of the video not the &autoplay… you’ll code that into the args part of the function.
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.
This can be easily fixed by modifying the wp_oembed_get() function in wp-includes/media.php to this: