Skip to content

Instantly share code, notes, and snippets.

@hslaszlo
Forked from Clorith/functions.php
Created June 15, 2021 14:57
Show Gist options
  • Save hslaszlo/095e2f7b3548843a1f4217d272c5e5d7 to your computer and use it in GitHub Desktop.
Save hslaszlo/095e2f7b3548843a1f4217d272c5e5d7 to your computer and use it in GitHub Desktop.
WordPress filter for adding responsive wrapper to embedded content
<?php
/**
* Filter for adding wrappers around embedded objects
*/
function responsive_embeds( $content ) {
$content = preg_replace( "/<object/Si", '<div class="embed-container"><object', $content );
$content = preg_replace( "/<\/object>/Si", '</object></div>', $content );
/**
* Added iframe filtering, iframes are bad.
*/
$content = preg_replace( "/<iframe.+?src=\"(.+?)\"/Si", '<div class="embed-container"><iframe src="\1" frameborder="0" allowfullscreen>', $content );
$content = preg_replace( "/<\/iframe>/Si", '</iframe></div>', $content );
return $content;
}
add_filter( 'the_content', 'responsive_embeds' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment