WordPress – Retrieve IMG’s from the_content and clean it

I’m trying to build a function for my WordPress website, which would retrive all IMG’s from the_content, and clean it with a bunch of operations:

  • removing CLASS (you know, e.g. class=”alignnone size-full wp-image-707″)
  • replacing ALT with the post’s title + a number to avoid duplicate ALTs
  • indenting to match my clean-code-obssession

When I echo the_content, I call my ‘clean_image_set’ function:

Read More
add_filter("the_content", "clean_image_set"); the_content();

The function looks like this:

function clean_image_set($html) {
  $html = preg_replace( '/(class=")(.*?)(")/i', "class="img"", $html ); 
  $post_title = get_the_title();
  $html = preg_replace( '/(alt=")(.*?)(")/i', '$1'.esc_attr( $post_title ).'$3', $html ); // replace alt with post title
  return $html;
}

I think I should use preg_match_all to put all the IMGs into an array and then use a loop to put a number in the ALT and add indentation, but can’t manage to do it.

Any thought?
Thanks a lot!

Related posts