WordPress.org

Forums

[resolved] how to write a filter to remove img tag? (6 posts)

  1. downFast
    Member
    Posted 5 years ago #

    Hi, i would like to write a filter that i can use in my template to remove img tags, any idea?

  2. Depends on where you want to remove the tag from. If it's the content of the posts then use the the_content filter and a preg_replace or whatever.

    ~ @kovshenin

  3. downFast
    Member
    Posted 5 years ago #

    i am trying to use this within the loop <?php remove_filter('the_content', '<img>');?> but it doens't work, am i doing something wrong?

  4. Of course, read more about what filter are and how to use them. You should be doing something similar to this:

    add_filter('the_content', 'my_content_filter');
    function my_content_filter($content)
    {
        $content = preg_replace('#(<[/]?img.*>)#U', '', $content);
        return $content;
    }

    Haven't tested this regex but it's the first one I found in Google using 'preg_replace img tag'. Anyways, you should be able to get the picture.

    ~ @kovshenin

  5. downFast
    Member
    Posted 5 years ago #

    superb it works :) thank you

  6. stefsegers
    Member
    Posted 4 years ago #

    Hi Kovshenin,

    Is it possible to do the opposite? So remove all data from The_Content except the IMG??? This way I can get only my images posted on my blog.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.