3

I would like to remove the [...] from the RSS feed widget.

I have tried adding the following to functions:

function replace_ellipsis($text) {

$return = str_replace('[...]', '-', $text);
 return $return;
}
add_filter('get_the_excerpt', 'replace_ellipsis');

However, this does not affect the widget.

Any help?

3
  • 1
    have you tried to use 'the_excerpt_rss' filter? or … in the string replace?
    – Michael
    Jul 26, 2012 at 13:48
  • @Micheal Should be an answer, not a comment.
    – kaiser
    Jul 26, 2012 at 18:55
  • Yes I've tried that.
    – user18577
    Jul 27, 2012 at 12:18

2 Answers 2

3

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!

2

The body of the default RSS widget is generated by the function wp_widget_rss_output. As you can see in the source, the [...] is hardwired into the code and echoed directly. There is no filter for straightforward manipulation of the feed output.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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