Created
November 11, 2013 19:27
-
-
Save radist2s/7418847 to your computer and use it in GitHub Desktop.
Wordpress Youtube embed autoplay and wmode=opaque filter
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('oembed_result', 'youtube_autoplay_filter', 10); | |
function youtube_autoplay_filter($iframe_html){ | |
$matches = array(); | |
if (!preg_match('~src="([^"]+?)"~iu', $iframe_html, $matches)) | |
{ | |
return $iframe_html; | |
} | |
$url = $matches[1]; | |
if (FALSE === mb_strpos($url, 'youtu')) | |
{ | |
return $iframe_html; | |
} | |
$url_suffix = ''; | |
if (false === mb_strpos($url, '?')) | |
{ | |
$url_suffix .= '?'; | |
} | |
else | |
{ | |
$url_suffix .= '&'; | |
} | |
$args = array( | |
'wmode' => 'opaque', | |
'autoplay' => 1, | |
'cc_load_policy' => 1, | |
'rel' => 0 | |
); | |
$url_suffix .= http_build_query($args); | |
$iframe_html = preg_replace('~src="([^"]+?)"~iu', 'src="$1' . $url_suffix . '"', $iframe_html); | |
return $iframe_html; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment