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;
?>
.
.
.



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

Comment RSS · TrackBack URI

Leave a comment

Name: (Required)

Email: (Required)

Website:

Comment: