Support

Account

Home Forums ACF PRO oEmbed params

Solved

oEmbed params

    • Bobz

    • August 14, 2014 at 6:31 pm

    Hi,

    Im using ACF 5 and oEmbed field to embed some Vimeo videos.
    Videos I need to control over javascript so I need to add param ?api=1 in url so url looks like: http://player.vimeo.com/video/100751417?api=1 but looks like this part is stripped somewhere and it’s not printed out?

  • Try this, it edits the iframe tag with PHP

    <?php
    $html = get_field('video');
    $dom = new DOMDocument;
    $dom->loadHTML($html);
    foreach ($dom->getElementsByTagName('iframe') as $item) {
        $src = $item->getAttribute('src');
        $srcEx = explode('video/', $src);
        $item->setAttribute('id', 'v-' . $srcEx[1]);
        $item->setAttribute('src', $item->getAttribute('src') . '?api=1&player_id=v-' . $srcEx[1]);
        print $dom->saveHTML();
    }
    ?>

    You can obviously change the way you assign the ID 😉

    • Elliot

    • September 2, 2014 at 12:13 pm

    Hi @bobz

    You could use a preg_replace to find the src=”” part of the embed, and then add in the extra ?api=1 like so:

    
    $value = get_field('video');
    $value = preg_replace('/src="(.+?)"/', 'src="$1?api=1"', $value);
    

    Thanks
    E

    • ratatat

    • April 28, 2015 at 9:05 pm

    Actually to the code that I found in the documentation for Oembed, I just added

    			$params = array(
    				'controls'    => 0,
    				'hd'        => 1,
    				'autohide'    => 1,
    				'autoplay' => 1
    				);

    and it worked perfectly. Maybe you modified this in the code? but this solution its very intuitive and it works good.

  • Where did you add that code in what file?

  • Forgot to say I use Timber, and Twig to show the ACF field so that might be an issue anyone knows how to add attributes to that? Also, why don’t the field have attributes field coded in so we can add it in the backend?

    • radgh

    • September 24, 2018 at 4:30 am

    Ratatat’s solution worked perfect for me. This is right out of my page template, the oembed is named “video”:

    	$video = get_field( 'video' );
    	
    	if ( $video ) {
    		// Add autoplay functionality to the video code
    		if ( preg_match('/src="(.+?)"/', $video, $matches) ) {
    			// Video source URL
    			$src = $matches[1];
    			
    			// Add option to hide controls, enable HD, and do autoplay -- depending on provider
    			$params = array(
    				'controls'    => 0,
    				'hd'        => 1,
    				'autoplay' => 1
    			);
    			
    			$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);
    		}
    	
    		echo '<div class="video-embed">', $video, '</div>';
    	}
    • baomp

    • September 25, 2018 at 6:29 am

    @webthemer – Did you manage to find how to add autoplay for oEmbed with Timber?

  • Add this to functions.php file:

    add_action( 'run_video_embed', 'video_embed' );
    
    function video_embed( $context ) {
    
        $video = get_field( 'media' );
    	
    	if ( $video ) {
    		// Add autoplay functionality to the video code
    		if ( preg_match('/src="(.+?)"/', $video, $matches) ) {
    			// Video source URL
    			$src = $matches[1];
    			
    			// Add option to hide controls, enable HD, and do autoplay -- depending on provider
    			$params = array(
    				'controls'    => 1,
                    'hd'        => 1,
                    'fs'        => 1,
                    'rel'        => 0,
                    'modestbranding' => 1,
    				'autoplay' => 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);
    		}
    	
    		echo '<div class="video-embed">', $video, '</div>';
    	}
    }

    and add this in Twig

    {% do action('run_video_embed') %}

  • Anyone else doing this experiencing problems with Vimeo oEmbeds? If I have the ‘controls’ parameter set to 0, it just shows a thumbnail background and users can’t view the videos, titles, author, etc… basically it’s just an image at this point.

Viewing 10 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.