How to exclude images from wordpress post


In one of project I got requirement of to exclude images from WordPress post.

I written following code for excluding the image from Post

Above line I added in showing post without image.

<?php
$FormatedContent = content_with_formatting();
$content = the_content();
$postWithoutImage = preg_replace(‘/<img[^>]+./’,” $FormatedContent);
echo $postWithoutImage;
?>
In above code we are checking the image tag and replacing with none.

If you want to find all images from post then Use following command.
preg_match_all(‘/<img[^>]+>/i’,YOUR_text, $result);

You need to add following function in your function.php file(Theme files).
function content_with_formatting($more_link_text = ‘(more…)’, $stripteaser = 0, $more_file = ”)
{
$content = get_the_content($more_link_text, $stripteaser, $more_file);
$content = apply_filters(‘the_content’, $content);
$content = str_replace(‘]]>’, ‘]]>’, $content);
return $content;
}

If you want to Hide all images using CSS then use following code
img{
display:none;
}

Just put above code in your style.css file.

Advertisements
This entry was posted in PHP and tagged by Purab Kharat. Bookmark the permalink.

About Purab Kharat

I am WordPress and Drupal Architect. I worked on many technologies like Ruby on Rails, Flex, PHP etc... I am Purab Kharat from India, Pune. I love to play chess and listen music and read books. I work in SY Smartsourcing Global Pvt Ltd in Princeton, NJ, USA. Now I am living at Plainsboro, 164 Hampshire Drive, Princeton Meadows, NJ. I have more then Ten years experience in Web Application Development (Specially in Php 5.0, Ruby on Rails, Mysql 5.0, RIA, Wordpress, Drupal, Joomla, Ecommerce)

3 thoughts on “How to exclude images from wordpress post

  1. hi there,

    couldn’t find any contact info for you so thought i would post here. I am trying to do a similar thing with posts but want to exclude text/numbers

    The reason why is that i’m having people post via email with mms from a mobile phone, the generated email that comes from the mobile provider contains “the following number ******** has sent you a picture. I don’t want to publish peoples phone numbers.

    Do you have any ideas? Haven’t been able to find this anywhere.

    Many thanks jon

  2. Hi,

    Your tutorial is good but there’s some error I’d like to point out.

    $FormatedContent = content_with_formatting();
    //$content = the_content(); //This line must be removed or the_content will still output
    $postWithoutImage = preg_replace('/]+./','', $FormatedContent);
    echo $postWithoutImage;

    Thanks.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s