How to remove first image from wordpress post with caption if caption is present

How to remove first image from wordpress post with caption

I struggled lot for this issue. Some times I only removed the first image. some times I removed first caption. Using following code you can easily remove the post first image and caption.

How to remove first image from wordpress post with caption if caption is present

I used the following code for first image removing.

/*
 * Removing the first image from post
 */
function remove_first_image ($content) {
 if (!is_page() && !is_feed() && !is_feed() && !is_home()) {
    if (preg_match("/<img[^>]+\>/i",$content)) {
		 $content = preg_replace("/<img[^>]+\>/i", "", $content, 1);
 	}
	return $content;
   }
}

But I faced the some issues with that code. When first image has caption then only image get removed and caption will remain there.

After that I used the following code for caption remove.

/*
 * Removing the first caption from post with image
 */
function remove_first_image_caption ($content) {
 if (!is_page() && !is_feed() && !is_feed() && !is_home()) {
 $content = preg_replace("(\)", "", $content, 1);
 } return $content;
}
add_filter('the_content', 'remove_first_image_caption');

But if first image has no caption and second image image has caption then both the images will be get removed from post.

After some struggle I written following code. Following is the solution:

Final Code.

/*
 * Removing the first image from post
 * functionality added to delete caption is present to first image.
 */
function remove_first_image ($content) {
 if (!is_page() && !is_feed() && !is_feed() && !is_home()) {
    if (preg_match("/<img[^>]+\>/i",$content)) {
    //find first image URL
     $first_img = '';
     $output = preg_match_all('/< *img[^>]*src *= *["\']?([^"\']*)/', $content, $matches);
     $first_img = $matches[1][0];

    //find first image caption inner text
     $output = preg_match_all("/caption=['"](.*)/", $content, $matches);

     //find first image present in first caption text or not
        $pos = strpos($matches[0][0], $first_img);

    // if image URL found in caption array then delete the caption and image
        if ($pos !== false) {
           $content = preg_replace("(\)", "", $content, 1);
        } else {
           $content = preg_replace("/<img[^>]+\>/i", "", $content, 1);
        }
    }

 } return $content;
}
add_filter('the_content', 'remove_first_image');

solved: pagination for Custom post type not working

Some people asked me about pagination of custom post type. That is very easy.

For showing the custom post type we always use the query_post method.

Just use the following code in your template or theme file.

<!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php get_header(); ?>

            <!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php  query_posts( 'post_type=custom-post-type&paged='.$paged );
                                    if (have_posts()) : while (have_posts()) : the_post(); ?>

loop here

              			<!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php endwhile; ?>
				  <div id="nav-below" class="navigation"></pre>
<div class="nav-previous"><!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php next_posts_link( __( '<span class="meta-nav">←</span> Older posts', 'twentyten' ) ); ?></div>
<pre></pre>
<div class="nav-next"><!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php previous_posts_link( __( 'Newer posts <span class="meta-nav">→</span>', 'twentyten' ) ); ?></div>
<pre>
				  </div><!-- #nav-below -->
				  <!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php endif; wp_reset_query(); ?>

<!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php get_footer(); ?>
remove first image from wordpress post with caption
remove first image from wordpress post with caption
How to remove first image from wordpress post with caption if caption is present

Published by

Purab

I am Purab from India, Software development is my profession and teaching is my passion. Programmers blog dedicated to the JAVA, Python, PHP, DevOps and Opensource Frameworks. Purab's Github Repo Youtube Chanel Video Tutorials Connect to on LinkedIn

One thought on “How to remove first image from wordpress post with caption”

  1. I think the final code for finding the first caption for image needs to be fixed as it returns error message ‘Parse error: syntax error, unexpected ‘]’

    Code in question:
    $output = preg_match_all(“/caption=[‘”](.*)/”, $content, $matches);

Leave a Reply

Your email address will not be published.