Shane Holden
Everyone has a story—this is mine.
Shane Holden

My Personal Blog

x
Please note: the opinions I express here are solely mine at the time of writing, and may or may not apply any longer. If reading something that I've written in the past may change your opinion or thoughts of me, please proceed no further.
Aakash Web Announcer plugin
Standard
0
0
Permalink

Remove Images From Posts In WordPress

by Shane Holden – July 24, 2011

The need to prevent images from displaying in the post content came up with some feedback to my Instapic theme.  The user was using the WordPress iPhone App, and while making a picture post, it automatically adds the image into the post content.  Since the theme uses just the featured image feature of WordPress to display the image, having the image inserted in the post content made it appear twice in the theme.  He didn’t mind the need to just delete the inserted image from the post content, but I can see where that could eventually get annoying, and I want the theme to ultimately work as flawlessly and easily as possible.

The function below prevents images from being displayed in the post content when it’s placed in the functions.php file of your theme, and the_content(); is changed to the_content_no_images();

// REMOVE IMAGES FROM POST CONTENT DISPLAY
function the_content_no_images() {
$content = get_the_content('');
$content = apply_filters('the_content', $content);
$pattern = '/()/';
$replacements='';
$removed = preg_replace($pattern, $replacements, $content);
echo $removed;
}

For some reason, my blog is removing part of the code. But put < i m g . + ? > with the spaces removed inside the parenthesis in the $pattern

It isn’t without it’s imperfections, as the image is still technically in the post content and would be displayed in the feed (unless you change it to show Summary instead of Full text)—it’s just prevented from being displayed, which does the trick in my case.


Possibly Related Posts

No Responses yet.

Got something to say?! Go for it!

Note: This post is over a year and a half old. Although it's not a fresh post, feel free to comment or share your thoughts anyway. I read and enjoy every comment that is posted here.