Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I want to allow commas in tag names? For example, "hello, world" or "portland, or" but Wordpress keeps separating them. I can do it from the categories page:

image

And it shows up fine. But anything added from the posts sidebar does not show up ok here:

image

There is some discussion on it here: http://core.trac.wordpress.org/ticket/14691 but looks like it may not get solved, at least for a while.

In the meantime, I'm looking for an easier solution than adding categories from the categories page.

I've tried searching plugins, and didn't see any that would be helpful. There are a few that deal with replacing commas with other characters when displaying a list of categories, or tags, but I don't see any plugins that allow the user to replace the default separator.

I don't care if I have to patch the core myself. Ideally I could write a plugin, but after looking through some of the code I can't figure out where this gets handled.

Does anybody have a solution, or tips on what functions and javascript to start hacking? I'm not sure where to start looking in the code.

share|improve this question
1  
Any luck on achieving this? I am also looking for a solution. –  supertrue Oct 20 '11 at 18:54
    
I've just added them from the categories page (as mentioned above). It's inconvenient but works. No luck on modifying the core or adding a filter (yet). –  cwd Oct 21 '11 at 15:22
1  
Never modify the core ;) –  Jared Jan 31 '12 at 16:42
1  
@Jared - it's OK to modify the core - as long as you submit your issue / patch to core.trac.wordpress.org and are OK with maintaining the patch on your installation until it (hopefully) gets pulled into the core ;) –  cwd Jan 31 '12 at 19:39
    
In that case, you're right. :) –  Jared Jan 31 '12 at 21:56
add comment

3 Answers

up vote 3 down vote accepted

No core hacking needed -- thanks to: HOOKS.

Hooks allow to fix the issue with a nice combination of

  • a filter replacing "--" by ", " before output
  • and an "if" loop to make sure the output is not also filtered for the admin interface :)
  • and finally you, saving all your tags with comma in the format "Fox--Peter" instead of "Fox, Peter"

Here's the code:

// filter for tags with comma
//  replace '--' with ', ' in the output - allow tags with comma this way
//  e.g. save tag as "Fox--Peter" but display thx 2 filters like "Fox, Peter"

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');
}

Maybe some additional details in my blog post to that topic help as well .. http://blog.foobored.com/all/wordpress-tags-with-commas/

Greets, Andi

share|improve this answer
    
This will break. The comma is not a fixed string, it can be translated. Use _x( ',', 'tag delimiter' ) to catch the real delimiter. –  toscho Jun 15 '12 at 13:24
    
I haven't tested it yet but it looks good - thank you –  cwd Jun 26 '12 at 18:23
add comment

Here is your solution laying. Pay attention to line 2614:

    /**
2588   * Updates the cache for Term ID(s).
2589   *
2590   * Will only update the cache for terms not already cached.
2591   *
2592   * The $object_ids expects that the ids be separated by commas, if it is a
2593   * string.
2594   *
2595   * It should be noted that update_object_term_cache() is very time extensive. It
2596   * is advised that the function is not called very often or at least not for a
2597   * lot of terms that exist in a lot of taxonomies. The amount of time increases
2598   * for each term and it also increases for each taxonomy the term belongs to.
2599   *
2600   * @package WordPress
2601   * @subpackage Taxonomy
2602   * @since 2.3.0
2603   * @uses wp_get_object_terms() Used to get terms from the database to update
2604   *
2605   * @param string|array $object_ids Single or list of term object ID(s)
2606   * @param array|string $object_type The taxonomy object type
2607   * @return null|bool Null value is given with empty $object_ids. False if
2608   */
2609  function update_object_term_cache($object_ids, $object_type) {
2610      if ( empty($object_ids) )
2611          return;
2612  
2613      if ( !is_array($object_ids) )
2614          $object_ids = explode(',', $object_ids);
2615  
2616      $object_ids = array_map('intval', $object_ids);
2617  
2618      $taxonomies = get_object_taxonomies($object_type);
2619  
2620      $ids = array();
2621      foreach ( (array) $object_ids as $id ) {
2622          foreach ( $taxonomies as $taxonomy ) {
2623              if ( false === wp_cache_get($id, "{$taxonomy}_relationships") ) {
2624                  $ids[] = $id;
2625                  break;
2626              }
2627          }
2628      }
2629  
2630      if ( empty( $ids ) )
2631          return false;
2632  
2633      $terms = wp_get_object_terms($ids, $taxonomies, array('fields' => 'all_with_object_id'));
2634  
2635      $object_terms = array();
2636      foreach ( (array) $terms as $term )
2637          $object_terms[$term->object_id][$term->taxonomy][$term->term_id] = $term;
2638  
2639      foreach ( $ids as $id ) {
2640          foreach ( $taxonomies  as $taxonomy ) {
2641              if ( ! isset($object_terms[$id][$taxonomy]) ) {
2642                  if ( !isset($object_terms[$id]) )
2643                      $object_terms[$id] = array();
2644                  $object_terms[$id][$taxonomy] = array();
2645              }
2646          }
2647      }
2648  
2649      foreach ( $object_terms as $id => $value ) {
2650          foreach ( $value as $taxonomy => $terms ) {
2651              wp_cache_set($id, $terms, "{$taxonomy}_relationships");
2652          }
2653      }
2654  }
2655  

Inside wp-includes/taxonomy.php. Have a good luck hacking the code. there isn't any hook. it's hard coded... Sorry. I think that hacking the code is your only option for now.

share|improve this answer
2  
Hacking the WP core is probably not a good idea. –  chrisguitarguy Feb 21 '12 at 0:27
2  
I know that! but he asked the question. I just gave the answer. –  Asaf Chertkoff Feb 21 '12 at 1:04
    
Yes, but hacking core is the wrong answer. -1 –  EAMann Feb 21 '12 at 2:51
2  
+1 - In my original post I did mention that hacking the core was acceptable in this case. I couldn't even find where I needed to hack the core to make this work. At least if I know this is possible I can work to develop a plugin or submit a proper ticket request asking for a hook or filter from the WP community. So, thank you @AsafChertkoff, even though I haven't tested this myself yet. –  cwd Feb 21 '12 at 4:06
    
@cwd, your welcome :). hope that helps. –  Asaf Chertkoff Feb 21 '12 at 15:59
add comment

you mau use a filter.

For example, if you want to Add a comma after each tag in a tag cloud, you can put the following in your functions.php

function myfunc_filter_tag_cloud($args) {
    $args['smallest'] = 18;
    $args['largest'] = 32;
    $args['unit'] = 'px';
    $args['separator']= ', ';
    return $args;
}
add_filter ( 'widget_tag_cloud_args', 'myfunc_filter_tag_cloud');
share|improve this answer
1  
The problem is not displaying them with commas, the problem is keeping the commas as part of the tag/category name when entering them (while editing the post). I'd like to figure out how to edit wordpress (or write a plugin) to allow me to do that. –  cwd Aug 7 '11 at 4:54
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.