When I add images to a post via the TinyMCE editor Iâd like WP to add a wrapping div around the <img>
tag like <div class="img-wrapper"><img src="url-to-image" alt="image" /></div>
. Is that somehow possible?
Leave a Reply
You must be logged in to post a comment.
I think it is a better idea, if you filter the output, the
the_content
and add the div on all images. It is better for feature, if you will remove this markup, it is not inside the database.To find and replace the img in content use a regex, like this
If you use the regex
preg_match_all( '/<img [^>]*src="([^"]+)"[^>]*>/', $content, $arr, PREG_PATTERN_ORDER);
then you get a array. Inside this array you find the img tag with all values,$arr[0]
. Enhance this var with your markup and return to the var$content
. Also here a example from the web. Additional hint for a regex tester, there I like: Regex Test ToolBut it is also possible to add markup on insert images in the editor: