Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End WordPress – Remove Paragraph tag around Images w/o Captions, Replace with Divs Reply To: WordPress – Remove Paragraph tag around Images w/o Captions, Replace with Divs

#156467
G M
Participant

Been playing around for a couple days and I think perhaps a php preg_replace might do the trick?

I don’t quite understand the process yet, but so far I have found this code and tried to modify it:

function filter_ptags_on_images($content) { 
  return preg_replace('/<p>\s*(<a .*>)?\s*(<img .* \/>)\s*(<\/a>)?\s*<\/p>/iU', 
  '/<div class="wp-nocaption">\s*(<a .*>)?\s*(<img .* \/>)\s*(<\/a>)?\s*<\/div>/iU', 
  $content); 
  }

add_filter('the_content', 'filter_ptags_on_images');

But in place of an image it returns this:

/
<div class="wp-nocaption">
 \s*(
 <a .*="">
      )?\s*(
    <img \="" .*=""></img>
      )\s*(<\/a>)?\s*<\/div>/iU
    </a>

Can anyone tell me what I’m doing wrong? Thanks!