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.


17 Comments so far »

  1. Lee Whitfield said

    September 13 2008 @ 12:41 am

    Fantastic, been looking for something like this for a while now. Thankyou

  2. Pete said

    November 15 2008 @ 12:43 pm

    Thank you!!!!

  3. Vitalsine said

    November 20 2008 @ 11:47 am

    You are friggin amazing man. Ive been looking everywhere for this!!!

  4. 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!

  5. 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!

  6. 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…

  7. 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 [...]

  8. weston said

    June 23 2009 @ 4:38 pm

    hey thanks for this! works great!

  9. 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.

  10. tootapi said

    July 27 2009 @ 12:58 am

    thx alot buddy

    thats a gr8 work thank you

  11. 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 :D

    once again, thanks! :D

  12. 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.

  13. Leannekera said

    October 14 2009 @ 5:52 am

    Thankyou for this quick and easy fix.

  14. 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.

  15. 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!

  16. 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 [...]

  17. Chris Schuld said

    November 14 2009 @ 2:04 pm

Comment RSS · TrackBack URI

Leave a comment

Name: (Required)

Email: (Required)

Website:

Comment: