Skip to content

Instantly share code, notes, and snippets.

@radist2s
Created November 11, 2013 19:27
Show Gist options
  • Save radist2s/7418847 to your computer and use it in GitHub Desktop.
Save radist2s/7418847 to your computer and use it in GitHub Desktop.
Wordpress Youtube embed autoplay and wmode=opaque filter
<?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