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'); |
@andri +1
What about the images outside of the post? Like images on the layout?
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
Slight issue, if you use a caption, the caption text ends up outside of the div. I would also use the figure element, thats what WP uses if an image has a caption.