Last active
November 28, 2016 02:56
-
-
Save levantoan/d6f5d7f9048de5a136ef to your computer and use it in GitHub Desktop.
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 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'); |
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
.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