WP Oembed not passing through the “autoplay=1” variable

I’m having a problem here:

I am passing this through a custom field: http://www.youtube.com/watch?v=E6P1Q-UycHA&autoplay=1

Read More

(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

Related posts

Leave a Reply

8 comments

  1. 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:

    wp_oembed_get( 'http://www.youtube.com/watch?v=', array( 'autoplay' => 1 ) );
    

    And filter:

    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;
    }
    
  2. 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+:

    /**
     * Adds the `autoplay` query string argument to embedded YouTube videos
     */
    function wpse5362_autoplay_youtube_oembed( $provider, $url, $args ) {
        if (strpos($provider, 'youtube')!==FALSE) {
            $provider = add_query_arg('autoplay', 1, $provider);
        }
    
        return $provider;
    }
    add_filter('oembed_fetch_url', 'wpse5362_autoplay_youtube_oembed', 10, 3);
    

    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 filter oembed_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.

  3. This is my solution in functions.php

    function embed_responsive_autoplay($code){
        if(strpos($code, 'youtu.be') !== false || strpos($code, 'youtube.com') !== false){
            $return = preg_replace('@embed/([^"&]*)@', 'embed/$1&showinfo=0&autoplay=1', $code);
            return '<div class="embed-container">' . $return . '</div>';
        }
        return '<div class="embed-container">' . $code . '</div>';
    }
    
    add_filter( 'embed_oembed_html', 'embed_responsive_autoplay');
    add_filter( 'video_embed_html', 'embed_responsive_autoplay' ); // Jetpack
    

    enjoy

  4. 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:

    <?php
    /**
     * Add parameters to embed
     * @src https://foxland.fi/adding-parameters-to-wordpress-oembed/
     * @src https://github.com/WordPress/WordPress/blob/ec417a34a7ce0d10a998b7eb6d50d7ba6291d94d/wp-includes/class-oembed.php#L553
     */
    $allowed_args = ['autoplay'];
    
    function koa_oembed_args($provider, $url, $args) {
        global $allowed_args;
    
        $filtered_args = array_filter(
            $args,
            function ($key) use ($allowed_args) {
                return in_array($key, $allowed_args);
            },
            ARRAY_FILTER_USE_KEY
        );
    
        foreach ($filtered_args as $key => $value) {
            $provider = add_query_arg($key, $value, $provider);
        }
    
        return $provider;
    }
    
    add_filter('oembed_fetch_url', 'koa_oembed_args', 10, 3);
    

    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:

    [embed autoplay="true"]https://vimeo.com/1234567890/1234567890[/embed]
    

    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.

  5. 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…

    /*
       N - replace with a valid post ID
       key - replace with a valid custom field key(name)
    */
    $data = get_post_meta( N, 'key', true );
    if( $data )
       echo apply_filters( 'the_content', $data );
    

    This of course will run several filters over the data that you don’t need, but it does the trick all the same..

  6. I don’t know if it’s needed any more, but I’ll post the answer here

    <?php
    /**
     * ACF oEmbed script w/ player parameters 
     * @link https://www.advancedcustomfields.com/resources/oembed/
     */
    
    $video = get_field('video');
    
    // use preg_match to find iframe src
    preg_match('/src="(.+?)"/', $video, $matches);
    $src = $matches[1];
    
    // add extra params to iframe src
    $params = array(
        'background'    => 1,
        'autoplay'      => 1,
        'controls'      => 0,
        'loop'          => 0,
        'color'         => '178395',
        'title'         => 0,
        'byline'        => 0,
        'portrait'      => 0,
        'rel'           => 0,
        'showinfo'      => 0,
    );
    
    $new_src = add_query_arg($params, $src);                
    $video = str_replace($src, $new_src, $video);
    
    // add extra attributes to iframe html
    $attributes = 'frameborder="0"';
    
    $video = str_replace('></iframe>', ' ' . $attributes . '></iframe>', $video);
    
    if($video) :
        echo '<div class='video-wrapper'>'.$iframe.'</div>';
    endif;
    

    also, as Gist: https://gist.github.com/zachakbar/3100e093cb8484d47586a259f9f960dc

  7. This can be easily fixed by modifying the wp_oembed_get() function in wp-includes/media.php to this:

    function wp_oembed_get( $url, $args = '' ) {
        // Manually build the IFRAME embed with the related videos option disabled and autoplay turned on
        if(preg_match("/youtube.com/watch?v=([^&]+)/i", $url, $aMatch)){
            return '<iframe width="560" height="315" src="http://www.youtube.com/embed/' . $aMatch[1] . '?rel=0&autoplay=1" frameborder="0" allowfullscreen></iframe>';
        }
    
        require_once( ABSPATH . WPINC . '/class-oembed.php' );
        $oembed = _wp_oembed_get_object();
        return $oembed->get_html( $url, $args );
    }