9

I'm grabbing a remote feed in my plugin and some entries have iframe code I want to keep. However, SimplePie fetch_feed keeps stripping it out. Here is my code and what I've tried already:

kses_remove_filters(); # remove kses filters but SimplePie strips codes anyway
$rss = fetch_feed( 'http://www.someblog.com/feed/' );
$rss_items = $rss->get_items( 0, 2 );  # get two entries for this example
foreach ( $rss_items as $item ) {
    # just dump to screen:
    echo "<div id='message' class='updated'><p>" .  $item->get_content() . "</p></div>";
}
kses_init_filters(); # remove kses filters but SimplePie strips codes anyway


# also tried adding iframe to kses_allowed_html filter:
function se87359_add_filter( &$feed, $url ) {
    add_filter('wp_kses_allowed_html', 'se87359_add_allowed_tags');
}
add_filter( 'wp_feed_options', 'se87359_add_filter', 10, 2 );
function se87359_add_allowed_tags($tags) {
    // Ensure we remove it so it doesn't run on anything else
    remove_filter('wp_kses_allowed_html', 'se87359_add_allowed_tags');
    $tags['iframe'] = array(
    'src' => true,
    'width' => true,
    'height' => true,
    'class' => true,
    'frameborder' => true,
    'webkitAllowFullScreen' => true,
    'mozallowfullscreen' => true,
    'allowFullScreen' => true
    );
    return $tags;
}

# also made sure not to cache the feed (for testing only):
function do_not_cache_feeds(&$feed) {
    $feed->enable_cache(false);
}
add_action( 'wp_feed_options', 'do_not_cache_feeds' );

# in case above doesn't work, set transient lifetime to 1 second:
add_filter( 'wp_feed_cache_transient_lifetime', create_function( '$a', 'return 1;' ) );
  • 1
    It would be helpful if you made example easily reproducible. You don't have to share link to original feed if it's private, but you could simply throw up example feed, demonstrating the issue, somewhere online like a gist. – Rarst Apr 2 '15 at 19:31
0

From the SimplePie docs here: the is a strip_htmltags property in the SimplePie object, which among others it has the iframe tag we want to keep.

So, apart from the wp_kses, probably we want to remove the tag from the above property.

For instance, the $rss = fetch_feed( 'http://www.someblog.com/feed/' ); gives us the SimplePie object.

If we var_dump($rss)

or even better "pretty print" it by using:

highlight_string("<?php\n\$rss =\n" . var_export($rss, true) . ";\n?>");

we will see all fetched entries and all properties of the $rss object. Among those there is the one we are looking for, and we can isolate it by using:

highlight_string("<?php\n\$rss->strip_htmltags =\n" . var_export($rss->strip_htmltags, true) . ";\n?>");

this will give us something like the below:

<?php
    $rss->strip_htmltags =
      array (
        0 => 'base',
        1 => 'blink',
        2 => 'body',
        3 => 'doctype',
        4 => 'embed',
        5 => 'font',
        6 => 'form',
        7 => 'frame',
        8 => 'frameset',
        9 => 'html',
       10 => 'iframe',
       11 => 'input',
       12 => 'marquee',
       13 => 'meta',
       14 => 'noscript',
       15 => 'object',
       16 => 'param',
       17 => 'script',
       18 => 'style',
     );
?>

From the above we note that the key of the iframe entry is 10. So we use array_splice to remove the entry, like:

// Remove these tags from the list
$strip_htmltags = $rss->strip_htmltags; //get a copy of the strip entries array
array_splice($strip_htmltags, 10, 1); //remove the iframe entry
$rss->strip_htmltags = $strip_htmltags; // assign the strip entries without those we want

Now the iframe entry is out and of the $strip_htmltags property and probably we are set.

Notice: I couldn't find a "test" rss feed containing some iframe to test the above. So if anyone can verify it, please provide some feedback.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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