Removing Images from a WordPress Post
Today I ran across a unique need to remove images from a WordPress post in a specific post loop. Because there is no way to do a “read more” excerpt while taking strict control of the raw content from the_content() I was limited to capturing and manipulating the content from PHP’s output buffer. My solution: obtain the output from the_content() and remove the image tags from the post using preg_replace().
Here is the solution:
. . . <?php ob_start(); the_content('Read the full post',true); $postOutput = preg_replace('/<img[^>]+./','', ob_get_contents()); ob_end_clean(); echo $postOutput; ?> . . .
EDIT: make sure you check out the updated version of this solution. Also, view my solution for removing everything but the images.
























Lee Whitfield said
September 13 2008 @ 12:41 am
Fantastic, been looking for something like this for a while now. Thankyou
Pete said
November 15 2008 @ 12:43 pm
Thank you!!!!
Vitalsine said
November 20 2008 @ 11:47 am
You are friggin amazing man. Ive been looking everywhere for this!!!
Rich said
December 30 2008 @ 4:38 pm
It’s almost like you read my mind and then created this! Should be perfect for a new portfolio blog theme I’m working on. Thanks man!
ilithya said
March 10 2009 @ 9:49 am
i luv u!!!!!!!!!!
so muchhh heheheh
i’ve been looking for a code like that and breaking my head for over a week!!!
– i’m a bit new to php and css…thus this was great!!!
thanks!
Lucas Smith said
April 20 2009 @ 2:41 pm
If you don’t have a tag in your content this breaks because of the true in the following:
the_content(‘Read the full post’,true);
hung me up for a bit wanted to let others know that if you remove it you’ll get all of the content and not just a if you don’t have a more tag…
Removing Images in a WordPress Post (Revisited) | Chris Schuld's Blog said
April 23 2009 @ 11:17 pm
[...] while back I wrote a semi-popular post on removing images from a WordPress post — today I am revisiting it. The original solution used the_content() and the output buffer to [...]
weston said
June 23 2009 @ 4:38 pm
hey thanks for this! works great!
Mike said
July 23 2009 @ 7:02 am
Nice work, but the title of this post was misleading for me in my search for something else.
I have images in my media gallery that are “assigned” or “attached” or “connected” to specific posts in my blog.
If I wanted to change the assignment to a different post, the way I would go about this would be to delete the image altogether and then re-upload it within the CMS of Wordpress, and then assign/attach it to a different post.
I thought your article would have information on how to remove the assignment and then possibly reassign an image to to another post, all within the CMS.
Shucks.
tootapi said
July 27 2009 @ 12:58 am
thx alot buddy
thats a gr8 work thank you
Fikri Rasyid @ Bloggingly said
August 27 2009 @ 12:54 am
Man, this post is really save me. Thanks! i was looking around this kind of “removing image from the_content” from using strip_tags to somethin else until finally i found this post
once again, thanks!
sandrar said
September 10 2009 @ 7:17 am
Hi! I was surfing and found your blog post… nice! I love your blog.
Cheers! Sandra. R.
Leannekera said
October 14 2009 @ 5:52 am
Thankyou for this quick and easy fix.
Richard said
November 6 2009 @ 6:34 pm
I never say thanks when i find solutions for my wordpress problems, but this time, u save me lot of work, Thank you sir. this code is exactly what i look for.
Thank you again.
Sebastian said
November 14 2009 @ 12:11 pm
Works great! Any idea how to remove all the text elements instead? I would like to keep the images to get kind of a lookbook. Thank you!
Removing everything BUT Images in a WordPress Post | Chris Schuld's Blog said
November 14 2009 @ 1:58 pm
[...] while back I wrote an article on Removing Images from a WordPress Post. Sebastian asked an interesting question; he wanted to remove everything but the images. This [...]
Chris Schuld said
November 14 2009 @ 2:04 pm
@Sebastian
Good question… here is the solution: http://chrisschuld.com/2009/11/removing-everything-but-images-in-a-wordpress-post/