I want to have commas display in tag names for city, county, and state names. Currently, they are separated, even when entering them into the tag editor (they later separate when trying to add them into a post).
I found an article that outlines how to cheat the system by using "--" as a placeholder for the comma and using a find and replace to turn it into a comma. https://www.saotn.org/display-commas-wordpress-tags/ However, when placing the code into functions.php and using a tag with "--" in its name, nothing happens. I see "--" still instead of the comma.
// 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' );
}