Removing Images from a WordPress Post

- 24 Aug 2008

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:

   the_content('Read the full post',true);
   $postOutput = preg_replace('/<img[^>]+./','', ob_get_contents());
   echo $postOutput;

EDIT : make sure you check out the updated version of this solution. Also, view my solution for removing everything but the images.

Related Posts: Extracting Smarty variables from source » Installing vzdump on CentOS 5.4 (32-bit) » Dumping Addresses from phplist » Adding NRPE (Nagios) to CentOS \/ RHEL Servers » Adding the REMI Repository to CentOS / RHEL / RedHat »

comments powered by Disqus