Last active
September 27, 2021 19:09
-
-
Save hagronnestad/5336369 to your computer and use it in GitHub Desktop.
WordPress: Wrap images in DIV.
This file contains hidden or 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
// ======================= | |
// = 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'); |
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
What about the images outside of the post? Like images on the layout?