Skip to content

Instantly share code, notes, and snippets.

@levantoan
Last active November 28, 2016 02:56
Show Gist options
  • Save levantoan/d6f5d7f9048de5a136ef to your computer and use it in GitHub Desktop.
Save levantoan/d6f5d7f9048de5a136ef to your computer and use it in GitHub Desktop.
<?php
/*
* Add to functions.php
* Add div.videoWrapper to iframe
*/
function div_wrapper($content) {
// match any iframes
/*$pattern = '~<iframe.*</iframe>|<embed.*</embed>~'; // Add it if all iframe*/
$pattern = '~<iframe.*src=".*(youtube.com|youtu.be).*</iframe>|<embed.*</embed>~'; //only iframe youtube
preg_match_all($pattern, $content, $matches);
foreach ($matches[0] as $match) {
// wrap matched iframe with div
$wrappedframe = '<div class="videoWrapper">' . $match . '</div>';
//replace original iframe with new in content
$content = str_replace($match, $wrappedframe, $content);
}
return $content;
}
add_filter('the_content', 'div_wrapper');
.videoWrapper {
position: relative;
padding-bottom: 56.25%; /* 16:9 */
padding-top: 25px;
height: 0;
}
.videoWrapper iframe,
.videoWrapper object,
.videoWrapper embed{
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment