I am creating a wordpress theme for a friend and am trying to add some markup before and after an image that has been inserted using the media button on a post.

Here is what I have so far but it’s giving me an error. Any suggestions or help would be much appreciated!

add_filter ('the_content', 'wpse264886_content_insert_html', 10, 1);
function wpse264886_content_insert_html ($content) {
  $html1 = 'before ... ';
  $html2    =  'after ...';
  $content = preg_replace('/(<a.*?)><(img.*?)/', '$1>' . $html1 . '<$2>' . $html2, $content);
  return $content;



Read more here: Changing output markup of an media file


If you know the solution of this issue, please leave us a reply in Comment section, to update the question.

Wordpress related questions and answers: