Skip to content

Instantly share code, notes, and snippets.

@Viper007Bond
Created November 27, 2012 08:35
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Viper007Bond/4153141 to your computer and use it in GitHub Desktop.
Save Viper007Bond/4153141 to your computer and use it in GitHub Desktop.
Auto-play YouTube embeds
<?php
add_filter( 'embed_oembed_html', 'blogsuccessjrnl_youtube_autoplay', 10, 4 );
function blogsuccessjrnl_youtube_autoplay( $html, $url, $attr, $post_ID ) {
// Abort if not in a single-post view. You don't want these auto-playing on you homepage.
if ( ! is_singular() )
return $html;
// Abort if not YouTube
if ( ! in_array( parse_url( $url, PHP_URL_HOST ), array( 'youtube.com', 'www.youtube.com' ) ) )
return $html;
// Add &autoplay=1 onto the end of the URL
$html = preg_replace( '#src="([^"]+)"#', 'src="$1&autoplay=1"', $html );
// This is a filter, so you MUST return the variable
return $html;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment