How to display WordPress tags with commas in them? Normally, in a WordPress post all tags are comma seperated: php, wordpress, functions.php. But what if you want to use a tag with commas in it? For instance cafe, bar, restaurants. Easy, create a filter in your WordPress functions.php, here is how.

Commas in WordPress tags

Sometimes you may want to display commas in tag names. For example if you have a business directory listing and want to create one single tag cafe, restaurant, bar. This post shows you how to create a filter in your functions.php file to display WordPress tags with a comma, enjoy!

Found on WordPress Development Stack Exchange, and is an over 5 year old enhancement request on Trac: allow commas in tag names.

Open up your theme’s functions.php file, and add the following code:

<?php
/* WordPress filter for tags with commas
 * replace '--' with ', ' in the output - allow tags with comma this way 
 * e.g. save tag as "Fox--Peter" but display like "Fox, Peter" 
 * or "cafe--restaurant" for "cafe, restaurant"
 *
 * Follow me on Twitter: @HertogJanR
 */

// make sure the filters are only called in the frontend
if( !is_admin() ){
    function comma_tag_filter( $tag_arr ){
        $tag_arr_new = $tag_arr;
        if( $tag_arr->taxonomy == 'post_tag' && strpos( $tag_arr->name, '--' ) ) {
            $tag_arr_new->name = str_replace( '--', ', ', $tag_arr->name );
        }
        return $tag_arr_new;    
    }
    add_filter( 'get_post_tag', 'comma_tag_filter' );

    function comma_tags_filter( $tags_arr ) {
        $tags_arr_new = array();
        foreach( $tags_arr as $tag_arr ) {
            $tags_arr_new[] = comma_tag_filter( $tag_arr );
        }
        return $tags_arr_new;
    }
    add_filter( 'get_terms', 'comma_tags_filter' );
    add_filter( 'get_the_terms', 'comma_tags_filter' );
}

(code by “Andi” on WordPress Development Stack Exchange)

Now when you save a tag with a double dash (--), the double dash will be displayed as a comma (,). So you’re kinda cheating in the presentation :-)

This may interest you:   17+ Valuable WordPress snippets you never knew you couldn't live without

Extend the filter to display custom taxonomies comma separated

get_the_taxonomies() Retrieve all taxonomies associated with a post.

Jason, founder of Pretty Code Machine, asked a question in the Advanced WordPress Facebook group about how to display a comma separate custom taxonomy.

What he wanted to create is:

Comma separate taxonomies in WordPress

Comma separate taxonomies in WordPress

Of course without saving two Locations taxonomies (Brattleboro and VT), but one: Brattleboro, VT.

I replied and pointed him to this post and filter mentioned above.

After we had some further contact back and forth, Jason came up with the following code to include a taxonomy filter to display the comma separated custom taxonomy for Locations he needed:

// filter for tags (as a taxonomy) with comma
//  replace '--' with ', ' in the output - allow tags with comma this way
if( !is_admin() ) { // make sure the filters are only called in the frontend
    $custom_taxonomy_type = 'location'; // here goes your taxonomy type
    
    function comma_taxonomy_filter( $tag_arr ){
        global $custom_taxonomy_type;
        $tag_arr_new = $tag_arr;
        if( $tag_arr->taxonomy == $custom_taxonomy_type && strpos( $tag_arr->name, '--' ) ){
            $tag_arr_new->name = str_replace( '--' , ', ', $tag_arr->name);
        }
        return $tag_arr_new;    
    }
    add_filter( 'get_' . $custom_taxonomy_type, comma_taxonomy_filter );
    
    function comma_taxonomies_filter( $tags_arr ) {
        $tags_arr_new = array();
        foreach( $tags_arr as $tag_arr ) {
            $tags_arr_new[] = comma_taxonomy_filter( $tag_arr );
        }
        return $tags_arr_new;
    }
    add_filter( 'get_the_taxonomies', 'comma_taxonomies_filter' );
    add_filter( 'get_terms', 'comma_taxonomies_filter' );
    add_filter( 'get_the_terms', 'comma_taxonomies_filter' );
}

Pretty neat, huh?! :)