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>
  • What is adding the iframe? Is it a plugin, a shortcode, manually entered in the HTML editor? – EAMann Jan 16 '13 at 17:50
  • Manually entered :) – versvs Jan 16 '13 at 17:55
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.

  • thanks, i will try this :) i'm not that skilled with regex and couldnt come up with a solution myself :) – versvs Jan 16 '13 at 18:53
  • It didnt work, it leaves a completely cleared rss :S – versvs Jan 16 '13 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 Jan 16 '13 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 Jan 16 '13 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 – yankitwizzy Sep 12 '13 at 23:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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