Remove first image from a wordpress post(the_content)

So tired… soul searching… I was looking for a solution to remove the first image from wordpress posts. I’ve found some, but they didn’t work. So, I decided to make my own code:

Just put it on your “functions.php” file.

function clear_first_image($content) {
	/*
		Warning:
		# I know light sabers are better...
			...but maybe I'm not good enough, so I use laser guns :)
		# It is not an elegant solution, but it works
		# Maybe someday I'll change it to use more regular expressions than logic
		# It's not recommended use this solution when you are
		processing large amount of data, like when you are on a loop
		# To call it on your code, under a loop(like "while" or "for"), use:
			echo clear_first_image(get_the_content())

		# Good luck! May the force be with you!
		alvaron8@gmail.com
	*/
	// checks if there is a first image
	$postst = strpos($content, '<img');
	if($postst !== false):
		// #letz check if the image has a link and remove it
		$output = preg_match_all('/<a.+href=[\'"]([^\'"]+)[\'"].*><img.+src/i', $content, $matches);
		$link_img = $matches [1] [0];
		if($link_img!=""):
			// we get the first link 'begin' tag
			$lnkini = strpos($content , '<a href', 0);
			// then we get the first link 'close' tag
			$lnkend = strpos($content, '<img', $lnkini);
			// letz get the hole first link tag now
			$replnk = substr($content, $lnkini, ($lnkend-$lnkini));
			// now we replace the first link tag in the content
			$tmpctd = preg_replace("'".$replnk."'",'',$content);
			// letz clear the link tag close
			$lnkcld = strpos($content , '</a>', 0);
			$content = substr($tmpctd, 0, $lnkcld).substr($tmpctd, $lnkcld, strlen($tmpctd));
		endif;
		// #removing the first image
			// we get the first image 'open' tag
			$posini = strpos($content , '<img', 0);
			// then we get the first image 'close' tag
			$posend = strpos($content, '>', $posini);
			// letz get the hole first image tag now
			$repimg = substr($content, $posini, ($posend-$posini)+1);
			// now we replace the first image tag in the content
			$postOutput = preg_replace("'".$repimg."'",'',$content);
	else:
		$postOutput=$content;
	endif;
	return $postOutput;
}

And call it on your code, like this:

echo clear_first_image(get_the_content());

,

  1. #1 by Kean on January 12, 2011 - 11:12 am

    Thanks for the function, it has come in rather handy for a project I’m working on. One issue I came across was where you used strrpos(). This seems to have issue if the post contains any other html coding, I found changing these to strpos() seemed to solve the issue.

    • #2 by Alvaro Neto on January 12, 2011 - 11:33 am

      Kean, I’m glad my solution was usefull for you. You are right about the strrpos issue, thank you. I’ll rewrite the function, test, and put it here.

  2. #3 by kistian on February 4, 2011 - 4:28 pm

    I have and error.
    If there isn’t an image in the content, get_the_content doesn’t work

    • #4 by Alvaro Neto on February 4, 2011 - 5:13 pm

      Kistian, you were right, there was a issue on the function. I changed a couple of things and tested many ways. Would please try to use it now? Please let me know about any possible new issue. Best regards.

  3. #5 by kistian on February 4, 2011 - 4:39 pm

    I asume that is necesary delete the function with the echo clear_first_image(get_the_content());

  4. #6 by Alvaro Neto on February 4, 2011 - 4:43 pm

    Kistian, thanks for your comments, I’ll check it out as soon as possible, and let you know. When I’ve tested, I thought did it with two situations, with one or more image and no image on the content, I’ll check it.

  5. #7 by Ruud on February 4, 2011 - 8:03 pm

    Does this work for images linked from blogger?
    Apparently no, but anyway is a great trick

    • #8 by Alvaro Neto on February 4, 2011 - 8:11 pm

      Ruud, I’ve tested it right now, and it’s working, with a image linked from blogger. Please let me know about any issue, if you use my function. Best Reagards.

  6. #9 by kistian on February 4, 2011 - 8:37 pm

    It’s work for me.
    Thanks a lot for everithyng Alvaro!

    • #10 by Alvaro Neto on February 5, 2011 - 3:16 am

      I’m very glad to know it’s working, Kistian. Thank you for visiting my blog. Best regards.

  7. #11 by kistian on February 7, 2011 - 1:11 pm

    when you see an entry whit get_the_content you loose all grammatical rules like full stop in the paragraphs, if you use it, the result is paragraph all mix together and that seems ugly.

  8. #13 by kurima on May 6, 2011 - 6:54 am

    Hi, been looking this code, however I need to remove second image on post instead of first image, so what code to remove second image?

    Thanks

    • #14 by Alvaro Neto on May 6, 2011 - 8:23 pm

      Hi Kurima, I can’t tell you now, But I’ll try to take a look asap and let you now.

Leave a reply to kistian Cancel reply