Removing Images in a WordPress Post (Revisited)

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:


16 thoughts on “Removing Images in a WordPress Post (Revisited)

  1. Pingback: Removing Images from a WordPress Post | Chris Schuld's Blog

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

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

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

  5. Commentators: a noteworthy endorsement. I’ve worked with Chris for 7+ years…great mind…good soul…remarkable code! Hire him ASAP. Donald Teel.

  6. Pingback: Remove images from Wordpress loop « labs.steveottenad.com

  7. Hi Chris,

    Thanks for a good solution I’ve been searching for! I’m having an issue that others have posted about; the image is removed, however all of my formatting is also removed, so I am left with text with no paragraph breaks and things like that. I’m not a very good coder, so I was wondering if you may have a fix for that issue? Thanks so much!

  8. great solution, but is there any way to remove IMG and other EMBED tag as well? Not a coder, hope u guys can help out. thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>