1

There is some iframe inside post content and I want to get rid of that iframe when the rss feeds are generated. To generate rss feeds, I'm showing full post content.

I started looking at strip_tags() but it does pretty much the opposite: strip everything unless the desired tags.

I'm wondering that maybe there is some hook and some wordpress-y form of solving this issue.

I know I should do something like this:

function rss_noiframe($content) {
    global $post;
    // filter out iframe here
    return $content;
    }

add_filter('the_excerpt_rss', 'rss_noiframe');
add_filter('the_content_feed', 'rss_noiframe');

But I'm lost at this point. Any ideas?

On demand by EAmann, here is some example code that isnt modified at all (it still appears in the feed, in fact):

<iframe src="http://somedomain.net/path/to/frame" frameborder="0" scrolling="no" width="500" height="375"></iframe>
2
  • What is adding the iframe? Is it a plugin, a shortcode, manually entered in the HTML editor?
    – EAMann
    Commented Jan 16, 2013 at 17:50
  • Manually entered :)
    – versvs
    Commented Jan 16, 2013 at 17:55

1 Answer 1

1

One potential option is to use preg_replace to do a regex match on your content and replace the iframe with empty space.

So this function:

function rss_noiframe($content) {
    $content = preg_replace( '/<iframe(.*)\/iframe>/is', '', $content );

    return $content;
}

add_filter('the_excerpt_rss', 'rss_noiframe');
add_filter('the_content_feed', 'rss_noiframe');

Should automatically convert any instances of <iframe src=...>...</iframe> to a blank.

5
  • thanks, i will try this :) i'm not that skilled with regex and couldnt come up with a solution myself :)
    – versvs
    Commented Jan 16, 2013 at 18:53
  • It didnt work, it leaves a completely cleared rss :S
    – versvs
    Commented Jan 16, 2013 at 19:46
  • 2
    Can you edit your answer to include some example HTML that has the iframe? All of my tests of the above code worked just fine ...
    – EAMann
    Commented Jan 16, 2013 at 19:55
  • Ok, sorry, it was my fault... Your code works indeed! I was calling the_content() to do further processing (add thumbnail) and that was overriding your regex.
    – versvs
    Commented Jan 16, 2013 at 21:31
  • I have the same issue but can't solve it. Please where do I place this function for it to be effective Commented Sep 12, 2013 at 23:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.