Back

Wordpress PHP

Wrap A Div Around An Iframe

In your functions.php, add in this code

<?php

function div_wrapper($content) {
    $pattern = '~<iframe.*</iframe>|<embed.*</embed>~';
    preg_match_all($pattern, $content, $matches);

    foreach ($matches[0] as $match) {
        $wrappedframe = '<div class="video-wrapper">' . $match . '</div>';
        $content = str_replace($match, $wrappedframe, $content);
    }

    return $content;
}
add_filter('the_content', 'div_wrapper');

?>