Chris Schuld's Blog

personal musings on the composition of software

Written By: Chris Schuld Thursday, April 23rd, 2009

A 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 remove the images out of the post. Now that I have used WordPress a bit longer and candidly had to use the solution again and thought “what was I thinking” I thought I would share the cleaner solution:

<?php echo preg_replace('/&lt;img[^&gt;]+./','',get_the_content()); ?>
Categories: Featured, PHP, WordPress

11 Responses to " Removing Images in a WordPress Post (Revisited) "

  1. Òscar says:

    Clean solution. Thanks for your work

  2. tootapi says:

    yuo thats pretty good

  3. ejjan says:

    How can I have wordpress to generate html tags, like . Using this method I get “plain” text in the post.

  4. Jeff says:

    where i must add the code?

  5. Cozzy says:

    I can’t see the code above, just the dots. Any chance of showing the code to do the updated version?

  6. Chris Schuld says:

    Cozzy:

    Fixed, thanks!

  7. ndamaren says:

    im just curious, this strips EVERYTHING and leaves behind and links to other content in the post, any way of working around this? im trying to use Fast Tube plugin, and it just leaves the link, but doesnt publish the video?

  8. daybreak says:

    great post chris, help me a lot :)

  9. Jay says:

    The regular expression is off.

    /<img[^>]+./ Will get rid of everything after an opening of an image tacg up until it hits something other than & or g or t or ;. The final dot is also useless and potentially dangerous since you can’t presume it’s a space.

    The more appropriate regex would be:

    /<img.*?(>)/

    You should also make it case-independent and test it with multi-line image tags.

    Appreciate your efforts but you may want to pick up a book on regular expressions.

  10. Jay says:

    Your blog converted my lt & gt into actual brackets. Comments should be converting ampersand characters into the appropriate HTML entity.

  1. [...] make sure you check out the updated version of this solution. Also, view my solution for removing everything but the [...]

Leave a Reply

About Chris

Chris Schuld

Thank you for visiting my website. I use this site for a myriad of things: maintaining software I have open sourced, connecting with my readers and friends and documenting all of this little things "I wished I would have written down." You can read more about me or contact me.


thanks for visiting... happy coding

Recent Comments