Feb
16
2009

Removing Images from a WordPress Post: Redux

by   |  Posted in Tutorials  |  19 comments

I wanted to remove the images from a WordPress post to give me more control of how I could layout the design for the front page of a website. After doing some messing around and failing, I finally found a great post by Chris Shuld which had an amazing little piece of code that solved my problem. I just had to make a few modifications to it.

<?php
$content = get_the_content();
$postOutput = preg_replace('/<img[^>]+./','', $content);
echo $postOutput;
?>

About the author:

A freelance web developer living in Montreal who spends most of his time writing for this site and building Premium themes for WordPress. You can find him on Twitter @bavotasan.

Site5 Affiliate Link
If you liked this, please share it.

Tags: , , , , , , , , , , , , ,

Short URL: http://bit.ly/aatn4H

Discussion 19 Comments

  1. Jason on April 30, 2009 at 11:01 pm

    THANK YOU!!!! I have never commented on a blog, but this little piece of code kicked ass!

  2. Mat??as on June 8, 2009 at 1:24 pm

    Hi. Your code really helps me! Just a question. It looks like displaying “plain” text. Do you know how can I get the html code in the content like p, a, h3, etc tags?

  3. Ray on July 21, 2009 at 3:49 pm

    Just use the following:

    wplistcategories(‘usedescfor_title=0′);

    print("wp_list_categories('use_desc_for_title=0'); ");

  4. Dyce on February 11, 2010 at 3:44 pm

    Do i put this code in the page or in the functions?

  5. Dyce on February 11, 2010 at 4:05 pm

    I’m trying to use this with an excerpt function, here is what I have:

    function the_content_limit($max_char, $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(']]&gt;', ']]&gt;', $content);
     
        $content = preg_replace('/]+./', '', $content); 
     
       if (strlen($_GET['p']) &gt; 0) {
     
          echo $content;
     
       }
     
       else if ((strlen($content)&gt;$max_char) &amp;&amp; ($espacio = strpos($content, " ", $max_char ))) {
     
            $content = substr($content, 0, $espacio);
     
            $content = $content;
     
            echo strip_tags($content);
     
       }
     
       else {
     
          echo $content;
     
       }
     
    }

    The excerpt never showed an image, but because of the image placement it didn’t show any text either.

    Now it’s only showing the 1st letter of the post.

  6. Jeff on March 1, 2010 at 2:03 am

    where i must add the code? wp_config.php?

    • c.bavota on March 1, 2010 at 1:59 pm

      You can add that code directly to the file where you want to call the content.

  7. Joel Lundgren on March 1, 2010 at 12:08 pm

    Thank you!
    No my search finaly ends.

  8. Kerry on June 21, 2010 at 5:27 pm

    I’m sorry, I send a comment about this subject and then see this page. thanks lot

  9. lesha on July 14, 2010 at 5:11 am

    Oh, your script is pretty useful ,Thank you so much!!:DDD

  10. Tyler on July 16, 2010 at 1:26 am

    Perfect!! Thanks

  11. no.quarter on October 20, 2010 at 6:41 am

    Hey…this is really easy way to do this. Using filter hook.

    //filter the content
    function content_strip_img($content) {
        return preg_replace("/]+\&gt;/i", "", $content);
    }
     
    add_filter('the_content', 'content_strip_img');

    And if you want you can use conditional tags if u want to display content without images just in front_page or category archive etc…

    • c.bavota on October 20, 2010 at 11:14 am

      Awesome. That’s some nice code. I just started getting into filters and actions and they are great.

    • Felix on December 4, 2010 at 7:51 am

      Please explain how to use this function. Sorry I’m a newbie :D
      Thanks.

  12. bJones on October 21, 2010 at 3:49 pm

    This is awesome code, We will be making this into a plug in to disable images in our blogs. Great work!

  13. Stoplink on October 25, 2010 at 1:24 pm

    I thing add_filter in the function is better

  14. jtmkrueger on December 21, 2010 at 1:40 pm

    This was VERY useful, thanks so much! I used this in conjunction with a function to let me place images:

    function getImage($num) {
     global $more;
     $more = 1;
     $link = get_permalink();
     $content = get_the_content();
     $count = substr_count($content, '&lt;img&#039;);
     $start = 0;
     for($i=1;$i&lt;=$count;$i++) {
     $imgBeg = strpos($content, &#039;');
     $postOutput = substr($post, 0, $imgEnd+1);
     $postOutput = preg_replace('/width="([0-9]*)" height="([0-9]*)"/', '',$postOutput);;
     $image[$i] = $postOutput;
     $start=$imgEnd+1;
     }
     if(stristr($image[$num],'&lt;img&#039;)) { echo &#039;&#039;.$image[$num].&quot;</a>"; }
     $more = 0;
     }

    then I just stick in a

     

    where I want images to appear from the post.

  15. jam on January 7, 2011 at 4:27 am

    thanks so much ! i need it because i using wordpress for e commercer