Your widget must have a method that looks something like this:
public function widget( $args, $instance ) {
echo $args['before_widget'];
if ( ! empty( $instance['title'] ) ) {
echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ). $args['after_title'];
}
wp_list_categories();
echo $args['after_widget'];
}
Apply your filter inside that method, then remove it:
public function widget( $args, $instance ) {
echo $args['before_widget'];
if ( ! empty( $instance['title'] ) ) {
echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ). $args['after_title'];
}
add_filter('wp_list_categories', 'cat_count_span');
wp_list_categories();
remove_filter('wp_list_categories', 'cat_count_span');
echo $args['after_widget'];
}
The same thing works with the backend form
method.