My general problem was: no commas in WP tags allowed
E.g. the tag
“Fox, Peter”
is not really allowed as a propper tag in wordpress – the reason ? The “add a tag to your post”-form adds the tags “comma separated”, which means such a tag like ours here
ends up as 2 tags: “Fox” and “Peter”
Here comes the solution: “Fox–Peter” + a filter
Yes, it is nowadays so simple.
We start off with saving all our “Fox, Peter” tags in the format “Fox–Peter”, as this is a simple, valid way to add a propper tag — and it is kind of unique to use “–“.
Then we write a few filters, hook them up in the right places and voilá, as soon as a tag is put out by your template, the “–” get replaced by nice looking “, “, put at !is_admin() around (to make sure your admin interface is still working properly) .. and
“Fox–Peter” becomes “Fox, Peter”
And here comes the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
// filter for tags 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 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); } |
Update 1: Now what about (custom) taxonomies ? Yes.
As tags are a kind of taxonomy this works also well for other types of taxonomy. You just have to adjust your filter to the respective taxonomy type name (also custom types allowed) and apply it to the respective input and output hooks:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
// 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 = 'post_tag'; // 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); } |
Enjoy!
Thanks so much for this – I managed to put commas by adding them in the tags interface and leaving them out when actually tagging, but it was way too buggy. Sometimes it worked as 1 tag, sometimes it split into two. This is perfect. Don’t want to have to check if the tag came out right everytime!!
Thanks, I’m glad if I could help.
Hi Andreas,
I’m a complete beginner with no knowledge of PHP except what I have briefly googled, and I have spent a few hours trying to figure out how to NOT apply this code to one specific page (where I have used a plugin to display names in an index, which requires the extra character to run). It’s working a little TOO beautifully, haha! Thank you for sharing it.
I have tried changing the conditional part to all manner of things that I thought would work, but to no avail. I thought that this would do it: if(!is_admin() && !is_page(646)) but it hasn’t worked and nor has anything else I’ve tried.
If you were able to help at all I would really appreciate it,
Kaitlin.
Hello Kaitlin,
actually your approach sounds quite fitting.
if(!is_admin() && !is_page(page_id)){
...
}
should do the job (given you set the correct page_id).
Feel free to send me a few more details via email and I’ll get back to you.
Br,
Andreas
Hi foo bored. Thanks for the great suggestion. Do you know if this works with custom taxonomies?
Hello Eric & Sean,
yes, this works also with taxonomies – see my updated posting.
Hi, the code works fantastic but I was wondering how I can use it for custom taxonomies? The plugin I use for custom taxonomy is http://wordpress.org/extend/plugins/custom-taxonomy-creator/
My taxonomies are authors, artists, and publishers. I’ve been trying to change word around in the codes and nothing works. I hope you’ll know the solution to it because it is such a fantastic code to allow comma in tags.
Thanks in Advance,
Sean
Where do we place this code? Thanks!
You’d have to place this code into your themes “functions.php” .. or any other “.php” file of your theme that is always called when a page with a tag (or taxonomy) is shown.
Hi.
Maybe you know How save term URL (tag/category) with “–” or “$mdash”?
I’m not aware about this problem. What is the issue with term URLs and “–” dashes ?