1

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;
}
  • Above code is working well. The problem was with caching plugin. Shame on me. – Yuri Jun 27 '17 at 0:00
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;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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