Remove first image from post wordpress

Remove first image from post WordPress. Some theme display post featured thumb image in post the_content, for such case need to remove first image from post using filter function.

When WordPress site theme change or upgrade and it starting display post featured image in post content, post page shows two same image if content have featured image in post.

In such cases updating all post not good idea, if site have large number of post with image in content.

For solution we can add filter function in theme function.php file, to remove first image from post.

Code example for remove first image with [ caption ] from post

If for seo purpose caption or description added, image will set by tag in post content. For remove it need to add in preg_replace process.


function remove_first_image ($content) {
  if (!is_home() && !is_page() && !is_feed()) {
    $content = preg_replace("#\s*\]*\].*?\[/caption\]\s*#is", "", $content, 1);
  }
  return $content;
}
add_filter('the_content', 'remove_first_image');

Code example for remove first image from post

If image have no caption in content, we can remove image by using <img tag preg_replace process.


function remove_first_image ($content) {
  if (!is_home() && !is_page() && !is_feed()) {
    $content = preg_replace("/]+\>/i", "", $content, 1);
  }
  return $content;
}
add_filter('the_content', 'remove_first_image');

!is_home() : Filter will not apply on home page.
!is_page() : Filter will not apply on normal page.
!is_feed() : Filter will not apply on feed.

If you want to also remove post content first image from any of place, you can remove such code from if condition.