我正在使用WordPress中提供的fetch\\u feed()函数来构建SimplePie提要对象。
WP的代码如下:
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;
}
如何修改在提要导入期间剥离的HTML元素?
SimplePie在其文档中说有一个函数strip\\u htmltags(),但我不确定如何在WordPress上下文中使用它。http://simplepie.org/wiki/reference/simplepie/strip_htmltags
以下是我尝试过但没有成功的方法:
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\' );
最合适的回答,由SO网友:Ryan McCue 整理而成
WordPress中的SimplePie使用内置的kses清理,而不是SimplePie。相反,您可以在wp_kses_allowed_html
并在那里添加您的元素。请记住,这将发生在all 后santization,而不仅仅是通过SimplePie。
function se87359_add_allowed_tags($tags) {
$tags[\'mytag\'] = array(\'myattr\' => true);
return $tags;
}
add_filter(\'wp_kses_allowed_html\', \'se87359_add_allowed_tags\');
如果您只想为feed执行此操作,那么以下操作应该可以:
/**
* 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;
}