WordPress SimplePie modifications

All we need is an easy explanation of the problem, so here it is.

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' );

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

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;
}

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply