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.
Fantastic, been looking for something like this for a while now. Thankyou
Thank you!!!!
You are friggin amazing man. Ive been looking everywhere for this!!!
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!
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!
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…
hey thanks for this! works great!
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.
thx alot buddy
thats a gr8 work thank you
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
once again, thanks!
Hi! I was surfing and found your blog post… nice! I love your blog.
Cheers! Sandra. R.
Thankyou for this quick and easy fix.
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.
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!
@Sebastian
Good question… here is the solution: http://chrisschuld.com/2009/11/removing-everything-but-images-in-a-wordpress-post/
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;
?>`
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.
thank you soo much.
Great tutorial, really helful! Cheers^^
Besides, it works with any other function.
Perfect solution for me! Thank you Chris!
Is there a way to also remove the caption div around the image (wp-caption)? 10x.
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;
?>
Hi
in the post the code is getting filtered… here trying to place with space..
‘/( ] * > ) /’
Sorry for a nooby question, Where must I add this ?
I’m quite new to these things
Please help me
Thanks.
How to execute this.. ? where to write this script
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