Skip to content

Instantly share code, notes, and snippets.

@hagronnestad
Last active September 27, 2021 19:09
Show Gist options
  • Save hagronnestad/5336369 to your computer and use it in GitHub Desktop.
Save hagronnestad/5336369 to your computer and use it in GitHub Desktop.
WordPress: Wrap images in DIV.
// =======================
// = WRAP IMAGES IN DIVS =
// =======================
function wrapImagesInDiv($content) {
$pattern = '/(<img[^>]*class=\"([^>]*?)\"[^>]*>)/i';
$replacement = '<div class="image-container $2">$1</div>';
$content = preg_replace($pattern, $replacement, $content);
return $content;
}
add_filter('the_content', 'wrapImagesInDiv');
@FrankPrendergast
Copy link

Thank you for this. Is there any possibility you would consider updating this to work with Gutenberg?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment