Created
November 27, 2012 08:35
-
-
Save Viper007Bond/4153141 to your computer and use it in GitHub Desktop.
Auto-play YouTube embeds
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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