WordPress: tags with commas and other / custom taxonomies

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:

 

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:

Enjoy!

11 thoughts on “WordPress: tags with commas and other / custom taxonomies”

    1. I’m not aware about this problem. What is the issue with term URLs and “–” dashes ?

    1. 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.

  1. 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

  2. Hi foo bored. Thanks for the great suggestion. Do you know if this works with custom taxonomies?

  3. 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.

    1. 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

  4. 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!!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">