Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am using the fetch_feed() function provided in WordPress to build a SimplePie feed object.

The code from WP is the following:

function fetch_feed($url) {
require_once (ABSPATH . WPINC . '/class-feed.php');

$feed = new SimplePie();

$feed->set_sanitize_class( 'WP_SimplePie_Sanitize_KSES' );
// We must manually overwrite $feed->sanitize because SimplePie's
// constructor sets it before we have a chance to set the sanitization class
$feed->sanitize = new WP_SimplePie_Sanitize_KSES();

$feed->set_cache_class( 'WP_Feed_Cache' );
$feed->set_file_class( 'WP_SimplePie_File' );

$feed->set_feed_url($url);
$feed->set_cache_duration( apply_filters( 'wp_feed_cache_transient_lifetime', 12 * HOUR_IN_SECONDS, $url ) );
do_action_ref_array( 'wp_feed_options', array( &$feed, $url ) );
$feed->init();
$feed->handle_content_type();

if ( $feed->error() )
    return new WP_Error('simplepie-error', $feed->error());

return $feed;
}

How can I modify which HTML elements get stripped during the feed import?

SimplePie in its documentation says that there is a function strip_htmltags(), but I'm not sure how I can use it within the WordPress context. http://simplepie.org/wiki/reference/simplepie/strip_htmltags

Here's what I tried, but didn't work:

function wpse87359_feed_options( $feed) {
$feed->strip_htmltags(array_merge($feed->strip_htmltags, array('h1', 'a', 'img','em')));
}
add_action( 'wp_feed_options', 'wpse87359_feed_options' );
share|improve this question
    
Anyone can help with this issue? –  drtanz Mar 2 '13 at 11:36
add comment

2 Answers

up vote 3 down vote accepted

SimplePie in WordPress uses the built-in kses sanitization, rather than SimplePie's. Instead, you can filter on wp_kses_allowed_html and add your elements there. Keep in mind that this will occur for all post santization, not just via SimplePie.

function se87359_add_allowed_tags($tags) {
    $tags['mytag'] = array('myattr' => true);
    return $tags;
}
add_filter('wp_kses_allowed_html', 'se87359_add_allowed_tags');

If you want to do it just for feeds, something like the following should work:

/**
 * Add in our filter when we run fetch_feed()
 */
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['mytag'] = array('myattr' => true);
    return $tags;
}
share|improve this answer
    
Hmm that's not so ideal, I want this to apply to just content gotten via SimplePie. –  drtanz Feb 19 '13 at 2:49
    
You'll need to do a setup where you only hook this in for feeds. There should be the appropriate hooks around, or if you can't figure it out, I can probably whip something up. –  Ryan McCue Feb 19 '13 at 11:40
    
I'm not really sure how I can do that, so if you could add that it would be great. –  drtanz Feb 20 '13 at 2:23
    
Do you think you could evolve the answer to work for feeds only? –  drtanz Feb 21 '13 at 1:35
    
Thanks for the edit. –  drtanz Mar 12 '13 at 2:40
add comment

Since you have the wp_feed_options hook in the above code example, you could try this to overwrite the array of stripped html tags (untested):

function wpse87359_feed_options( $feed) {
    $feed->strip_htmltags(array('html', 'style', 'body','iframe'));
}
add_action( 'wp_feed_options', 'wpse87359_feed_options' );

According to

http://xref.wordpress.org/tags/3.5.1/SimplePie/SimplePie_Sanitize.html#methodstrip_htmltags

the default tags to strip out are:

array(
    'base', 
    'blink', 
    'body', 
    'doctype', 
    'embed', 
    'font', 
    'form', 
    'frame', 
    'frameset', 
    'html', 
    'iframe', 
    'input', 
    'marquee', 
    'meta', 
    'noscript', 
    'object', 
    'param', 
    'script', 
    'style'
    )
share|improve this answer
    
That makes sense, but when I tried the code it didn't work. I tried removing img for example, but the imported feeds still have images. –  drtanz Feb 18 '13 at 16:35
    
Still can't get this to work, does it work on your end? –  drtanz Mar 3 '13 at 23:44
    
I don't think this will work as WordPress overrides the SimplePie sanitization class. –  drtanz Mar 13 '13 at 13:24
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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