Remove […] from RSS feed?

Here’s a suggestion:

  • First we target the RSS widgets that have summaries.
  • Then we filter the output of wp_trim_words() within the wp_widget_rss_output() function, to remove the […] part.

Then we clean up in two ways:

  • First we remove our wp_trim_words() filter, if it’s running, before the current widget is displayed. We can do that by hooking into widget_display_callback. That means our filter callback will not be active in the next widget.

  • Then we remove the filter callback, if it’s running, after the sidebar through the dynamic_sidebar_after hook. This means the filter callback will be removed after the dynamic sidebar. That could e.g. be useful if we only have a single widget in the dynamic sidebar.

Here’s the demo plugin:

add_filter( 'widget_display_callback', function( $instance, $obj, $args )
{
    // Cleanup before each widget
    if( has_filter( 'wp_trim_words', 'wpse_replace_hellip' ) ) 
        remove_filter( 'wp_trim_words', 'wpse_replace_hellip' );

    // Target RSS widgets with summary
    if( 
           'rss' === $obj->id_base 
        && isset( $instance['show_summary'] ) 
        && 1 == $instance['show_summary'] 
    ) {
        // Replace the […] part
        add_filter( 'wp_trim_words', 'wpse_replace_hellip' );
        // Clean up after dynamic sidebar
        add_filter( 'dynamic_sidebar_after', 'wpse_extra_cleanup' );
    }
    return $instance;
}, 10, 3 );

function wpse_replace_hellip( $text )
{
    if ( ' […]' == substr( $text, -11 ) )
        $text = substr( $text, 0, -11 );
    return $text;
}

function wpse_extra_cleanup()
{
    remove_filter( current_filter(), __FUNCTION__ );
    remove_filter( 'wp_trim_words', 'wpse_replace_hellip' );    
}

Hope you can test further and adjust to your needs!

Leave a Comment