You are here: » WordPress » Display commas in WordPress tags

Display commas in WordPress tags

Commas in tag name, like “cafe, bar, restaurants”

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

Allow commas in WordPress tag names

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 taxonomy (tag name) “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 :-)

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

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, isn’t it?! :)

3 thoughts on “Display commas in WordPress tags”

  1. Hi Jan- thank you for this extremely helpful post! One question- how would you go about extending your custom tax filter to accept an array of custom taxonomy names, instead of one? Thanks again.

  2. Thank you so much for posting this- particularly that last bit about dealing with the custom taxonomy. This saved the day!

Hi! Join the discussion, leave a reply!