admin管理员组

文章数量:1122826

One news aggregator wants to have RSS feed without any links into the <description> field. So I try to use this code in functions.php to remove <a href></a> tags, but it doesn’t work. What’s wrong? How can I remove the tags but keep intact all the text?

add_filter('the_content', 'my_custom_feed');
function my_custom_feed( $content ){
    global $post;

    if ( ! is_feed() )
        return $content;

    // Remove all shortcodes
    $content = strip_shortcodes( $post->post_content );
        $content = strip_tags( $content );

    // Remove all html tags, except these
    $my_allowed_tags = array(
        'p'      => array(),
        'strong' => array(),
        'em'     => array(),
        'img'    => array( 'src' => array(), 'width' => array(), 'height' => array() ),
    );
    $content = wp_kses( $content, $my_allowed_tags );

    // Balance tags
    $content = balanceTags( $content, true );

    return $content;
}

One news aggregator wants to have RSS feed without any links into the <description> field. So I try to use this code in functions.php to remove <a href></a> tags, but it doesn’t work. What’s wrong? How can I remove the tags but keep intact all the text?

add_filter('the_content', 'my_custom_feed');
function my_custom_feed( $content ){
    global $post;

    if ( ! is_feed() )
        return $content;

    // Remove all shortcodes
    $content = strip_shortcodes( $post->post_content );
        $content = strip_tags( $content );

    // Remove all html tags, except these
    $my_allowed_tags = array(
        'p'      => array(),
        'strong' => array(),
        'em'     => array(),
        'img'    => array( 'src' => array(), 'width' => array(), 'height' => array() ),
    );
    $content = wp_kses( $content, $my_allowed_tags );

    // Balance tags
    $content = balanceTags( $content, true );

    return $content;
}
Share Improve this question asked Jun 26, 2017 at 13:22 YuriYuri 311 silver badge2 bronze badges 1
  • Above code is working well. The problem was with caching plugin. Shame on me. – Yuri Commented Jun 27, 2017 at 0:00
Add a comment  | 

1 Answer 1

Reset to default 0

You haven't specified how it doesn't work :)

Does it remove some links or doesn't do anything at all. Anyway, to alter the content of every RSS item you can use rss_item() hook, or rss2_item() depending which stream is your target.

add_filter('rss_item', 'custom_item_content');
function custom_item_content($content) {

    //  alter your $content here

    return $content;
}

本文标签: How can I remove certain HTML tags from the RSS feed