• Resolved Lysefjordcam

    (@kmhaugen)


    Hi,
    Are there a way to get autoplay on youtube as Featured Video?
    It’s documented on youtube developer site that embedde videos can be given autoplay=1 parameter to start automatically. Maybe I can use the Tracks-child theme and the function.php to achieve this, but how? Please help.

    Regards
    Morten Haugen

    • This topic was modified 8 years, 5 months ago by Lysefjordcam.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Theme Author Ben Sibley

    (@bensibley)

    Hi Morten,

    Thanks so much for upgrading!

    There is a way we can filter the embed URL and add that parameter. The only catch is that this will make all of the Youtube Featured Videos autoplay. That said, I’ll see if I can add this as a selectable option in the next update.

    Please add the following code to your child theme’s functions.php file:

    <?php 
    
    remove_filter('oembed_result','ct_tracks_add_youtube_parameters', 10, 3);
    
    function ct_tracks_child_add_youtube_parameters($html, $url, $args) {
    
    	// access post object
    	global $post;
    
    	// get featured video
    	if( ! empty( $post ) ) $featured_video = get_post_meta( $post->ID, 'ct_tracks_video_key', true );
    
    	// only run filter if there is a featured video
    	if( ! empty( $featured_video ) ) {
    
    		// only run filter on the featured video
    		if( $url == $featured_video ) {
    
    			// only add parameters if featured vid is a youtube vid
    			if( strpos($featured_video, 'youtube.com' ) || strpos($featured_video, 'youtu.be' ) ) {
    
    				// get user Youtube parameter settings
    				// flip their value so 1 means, yes HIDE it, NOT yes SHOW it.
    				$youtube_title    = get_post_meta( $post->ID, 'ct_tracks_video_youtube_title', true ) ? 0 : 1;
    				$youtube_related  = get_post_meta( $post->ID, 'ct_tracks_video_youtube_related', true ) ? 0 : 1;
    				$youtube_logo     = get_post_meta( $post->ID, 'ct_tracks_video_youtube_logo', true ) ? 0 : 1;
    
    				$youtube_captions = get_post_meta( $post->ID, 'ct_tracks_video_youtube_captions', true );
    
    				$youtube_parameters = array(
    					'showinfo'       => $youtube_title,
    					'rel'            => $youtube_related,
    					'modestbranding' => $youtube_logo,
    					'cc_load_policy' => $youtube_captions,
    					'autoplay' => '1'
    				);
    
    				if ( is_array( $args ) ) {
    					$args = array_merge( $args, $youtube_parameters );
    				} else {
    					$args = $youtube_parameters;
    				}
    
    				$parameters = http_build_query( $args );
    
    				// Modify video parameters
    				$html = str_replace( '?feature=oembed', '?feature=oembed&' . $parameters, $html );
    			}
    		}
    	}
    
    	return $html;
    }
    add_filter('oembed_result','ct_tracks_child_add_youtube_parameters', 20, 3);

    Once added, the videos should autoplay.

    Thread Starter Lysefjordcam

    (@kmhaugen)

    Hi Ben,
    Thank you again for helping out. This is excellent for my use as I only have one video.
    If you’re able to add an option for this on selected videos it would be very useful if you have multiple videos sure!

    Regards
    Morten Haugen

    Theme Author Ben Sibley

    (@bensibley)

    Glad I could help! The auto-play option will be a great addition.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Autoplay parameter on Tracks Pro Featured Videos’ is closed to new replies.