Display commas in WordPress tags

Photo of author
Written By Jan Reilink

Windows Server systems administrator & enthusiast.

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…

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

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 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 language: HTML, XML (xml)

(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
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:

if( !is_admin() ) { $custom_taxonomy_type = 'location'; 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' ); }
Code language: PHP (php)

Pretty neat, isn’t it?! :)

Did you like: Display commas in WordPress tags

Then please, take a second to support Sysadmins of the North and donate!

Your generosity helps pay for the ongoing costs associated with running this website like coffee, hosting services, library mirrors, domain renewals, time for article research, and coffee, just to name a few.

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!

%d bloggers like this: