patiboh
5/17/2016 - 4:02 PM

WordPress - Filter <p> tags on post images (replace <p> by <figure>)

WordPress - Filter

tags on post images (replace

by )


/****** CHANGE THIS TO WP_AUTOP = false !!!! *********/

function filter_all_ptags_on_images($content){
  $count = 0;
  $new = preg_replace('/<p[^>]*>(.*?)(<img[^>]*>)(.*?)<\/p>/m', 
        '<p>$1</p><figure>$2</figure><p>$3</p>', $content, -1, $count);
  return $new;
}

add_filter('the_content', __NAMESPACE__ . '\\filter_all_ptags_on_images');

/* V2 : extract align classes from img and into figure */


function filter_all_ptags_on_images($content){
  $count = 0;
  $new = preg_replace('/<p[^>]*>(.*?)<img\s([^>]*)class="([\w-_\s]*?)(alignleft|alignnone)([\w-_\s]*?)"([^>]*)>(.*?)<\/p>/s', 
        '<p>$1</p><figure class="$4"><img $2 class="$3$4$5" $6></figure><p>$7</p>', $content, -1, $count);
  return $new;
}

add_filter('the_content', __NAMESPACE__ . '\\filter_all_ptags_on_images');