Q:

add a filter to add a class to tag link in wordpress

I want to add a filter to revise the link generated by get_the_tag_list in WP. It calls up get_the_term_list
function get_the_term_list( $id, $taxonomy, $before = '', $sep = '', $after = '' ) {
$terms = get_the_terms( $id, $taxonomy );

if ( is_wp_error( $terms ) )
    return $terms;

if ( empty( $terms ) )
    return false;

$links = array();

foreach ( $terms as $term ) {
    $link = get_term_link( $term, $taxonomy );
    if ( is_wp_error( $link ) ) {
        return $link;
    }
    $links[] = '<a href="' . esc_url( $link ) . '" rel="tag">' . $term->name . '</a>';
}

I want to add class="tag" but i'm not sure how to write a filter for my functions.php file to target only the $links[] bit of that function. Could I just exclude the old link set and add in my modified one somehow?
I was thinking to add something like this, but I have it wrong somehow:
add_filter('get_the_term_list','replace_content');
function replace_content($links[])
{
    $links[] = str_replace('<a href="', '<a class="tag" href="', $links[]);
    return $links[];
}

A:

You made a couple of mistakes. First add filter on get_the_term_list won't work, because it's not a filter. If you look in the code of get_the_term_list you'll see a line like this (depending on your WP version)
$term_links = apply_filters( "term_links-$taxonomy", $term_links );

So you can add a filter on term_links-$taxonomy in your case the taxonomy is tag.
The second mistake you made is the str_replace in combination with an array. If you want to use an array you don't need to add the [] after the variable. This is only for assigning the part after the = to the next item of an array. In this case you do a str_replace on the entire array so you should use $links instead of $links[] both in the assigning and in the str_replace otherwise you would add a new array (with the string replacement) after all the links of your current array.
add_filter( "term_links-post_tag", 'add_tag_class');

function add_tag_class($links) {
    return str_replace('<a href="', '<a class="tag" href="', $links);
}

