Chris Schuld's Blog

personal musings on the composition of software

Written By: Chris Schuld Sunday, August 24th, 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:

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


Categories: WordPress

37 Responses to " Removing Images from a WordPress Post "

  1. Lee Whitfield says:

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

  2. Vitalsine says:

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

  3. Rich says:

    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!

  4. ilithya says:

    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!

  5. Lucas Smith says:

    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…

  6. weston says:

    hey thanks for this! works great!

  7. Mike says:

    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.

  8. tootapi says:

    thx alot buddy

    thats a gr8 work thank you

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

  10. sandrar says:

    Hi! I was surfing and found your blog post… nice! I love your blog. :) Cheers! Sandra. R.

  11. Leannekera says:

    Thankyou for this quick and easy fix.

  12. Richard says:

    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.

  13. Sebastian says:

    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!

  14. frank says:

    often people just want to remove the first image in a post, with the above script, this is how.

    `<?php
    ob_start();
    the_content('Read the full post',true);
    $postOutput = preg_replace('/]+./’,”, ob_get_contents(),1);
    ob_end_clean();
    echo $postOutput;

    ?>`

  15. Chris says:

    Thanks! How can I update this function so that gallery items are left intact in the posts? My preg_replace knowledge is not good enough to do that.

  16. rizwan says:

    thank you soo much.

  17. Kazaf says:

    Great tutorial, really helful! Cheers^^
    Besides, it works with any other function.

  18. jfuste says:

    Perfect solution for me! Thank you Chris!

  19. footballclub says:

    Is there a way to also remove the caption div around the image (wp-caption)? 10x.

  20. Sougata says:

    Hi
    while trying to remove the first image from the the_content() with the above said process, it was not able to remove the first image after several try.

    I had simply replaced ..
    preg_replace(‘/]+./’,”, ob_get_contents(),1); with the one below..

    preg_replace(‘/(]*>)/’,”, ob_get_contents(),1);

    the and the final code works like charm! :-)

    here it is…

    <?php
    ob_start();
    the_content('Read the full post',true);
    $postOutput = preg_replace('/(]*>)/’,”, ob_get_contents(),1);
    ob_end_clean();
    echo $postOutput;
    ?>

  21. Sougata says:

    Hi

    in the post the code is getting filtered… here trying to place with space..

    ‘/( ] * > ) /’

  22. Shiva says:

    Sorry for a nooby question, Where must I add this ?
    I’m quite new to these things :P
    Please help me :)

    Thanks.

  23. Matt says:

    How to execute this.. ? where to write this script

  24. Chris Schuld says:

    All:

    You would place this in the theme php source for maybe home.php or index.php … replace the current “get_content();” with the source above.

    Good luck, Chris

  25. Thanh says:

    This is a very nice post, thanks alot Chris!!

  26. Raymond Ri says:

    Thanks for great point. over the year and year, it will help people. :D

  27. Kelly Tolden says:

    Wow that’s great. Interested in joining my blog team? We are a team and we all work together to help eachother and we would love you have you aboard.. We all help each other achieve our goals. Hope you are interested. Go to my landing page Global Domains International and when you leave your info I will get back to you. Get on board and take the FREE trial. Would love to have you aboard.

  28. legal says:

    Its working, great scripts.

  29. Thank you, man.

    FANSTAAAAASTIC ;)

  30. Thanks for the help Chris. You’ve saved me some major time.

  31. pet says:

    Thankz! You made my day

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

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

  3. [...] http://chrisschuld.com/2008/08/removing-images-from-a-wordpress-post/ <?php    ob_start();    the_content(‘Read the full post’,true);    $postOutput = preg_replace(‘/<img[^>]+./’,”, ob_get_contents());    ob_end_clean();    echo $postOutput; ?> [...]

  4. [...] source can be found here. Tweet Discussion /** * var disqus_identifier; [Optional but recommended: Define a unique [...]

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