15

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 http://img839.imageshack.us/img839/6869/picturepp.png

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

image http://img52.imageshack.us/img52/4950/picture1oax.png

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.

5
  • 1
    Any luck on achieving this? I am also looking for a solution.
    – supertrue
    Commented Oct 20, 2011 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
    Commented Oct 21, 2011 at 15:22
  • 1
    Never modify the core ;)
    – Jared
    Commented Jan 31, 2012 at 16:42
  • 2
    @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
    Commented Jan 31, 2012 at 19:39
  • In that case, you're right. :)
    – Jared
    Commented Jan 31, 2012 at 21:56

5 Answers 5

9

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" block to make sure the output is not also filtered for the admin interface :)
  • and finally, 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

3
  • This will break. The comma is not a fixed string, it can be translated. Use _x( ',', 'tag delimiter' ) to catch the real delimiter.
    – fuxia
    Commented Jun 15, 2012 at 13:24
  • I haven't tested it yet but it looks good - thank you
    – cwd
    Commented Jun 26, 2012 at 18:23
  • On your site you have the custom tax's too, I suggest you change the function names for anyone that can't figure it out. On the custom code you wrote: comma_tags_filter instead of 'comma_tags_filter' which quite much hurts the code. All in all great job.
    – David H
    Commented Jun 17, 2014 at 15:13
2

Commas in tag names won't work properly because, when adding the tag to a post, WordPress will parse it as multiple keywords, breaking at the comma.

A simple fix:

Use , (the HTML code for the comma) instead of a regular comma.

Then:

  • , will display as a nice comma inside of posts, tag page titles and a bunch of other places.
  • It will display raw, as ,, inside of the admin interface when typing the tag's name.

FYI, using the HTML entity, , does not work.

1

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.

5
  • 2
    Hacking the WP core is probably not a good idea. Commented Feb 21, 2012 at 0:27
  • 2
    I know that! but he asked the question. I just gave the answer. Commented Feb 21, 2012 at 1:04
  • Yes, but hacking core is the wrong answer. -1
    – EAMann
    Commented Feb 21, 2012 at 2:51
  • 3
    +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
    Commented Feb 21, 2012 at 4:06
  • @cwd, your welcome :). hope that helps. Commented Feb 21, 2012 at 15:59
1

It's possible and very easy to save tags with commas programmatically.

When calling wp_set_post_terms( $post_id, $terms, $taxonomy ), if you supply a string, it will be exploded into an array. If you supply your $terms as an array, each item in the array will be provided as a it's own term without being split into multiple terms.

// Example term with comma.
$terms = 'Surname, Given Names';
// Creates and/or assigns multiple terms separated by a comma.
wp_set_post_terms( $post_id, $terms, $taxonomy );
// Creates and/or assigns a single term with a comma.
wp_set_post_terms( $post_id, (array) $terms, $taxonomy );

Both wp_insert_post and subsequently wp_update_post use wp_set_post_terms when the $arg tax_input is set.

// Ensure $terms is an array.
$args['tax_input'][$taxonomy] = (array) $terms;
$post_id = wp_insert_post( $args );

The best way to create terms on the fly using the WordPress Dashboard UI, may require you to create your own meta box that submits any string including commas as a single term. Some plugins, such as ACF Pro, do this by default when you create a custom field to save the taxonomy, and select to also load and assign the terms when saved.

/* Example JSON config snippet for an ACF Pro Export/Import. */
/* Most likely config for most of these situations: "allow_null" */
/* and "field_type" may need to be adjusted depending on the situation. */
{
    "type": "taxonomy",
    "field_type": "multi_select",
    "allow_null": 1,
    "add_term": 1,
    "save_terms": 1,
    "load_terms": 1
}

Even when saved with a comma, each part of those terms with commas may still appear as separate items when editing the post. In this case it may be best to disable the default UI and rely on the custom meta boxes. This can be done using Screen Options when editing a post type. Custom taxonomies can also be hidden from the quick edit section when being registered.

// Register Custom Taxonomy args - disable default UI in quick edit.
$args['show_in_quick_edit'] = false;
register_taxonomy( $taxonomy, (array) $post_types, $args );
1
  • Just thought of a dirty fix (haven't tested it yet myself), but you could add a filter that will always sanitize the terms, by converting them all to an array. add_filter( 'pre_tax_input', function( $tax_input ) { return (array) $tax_input; } ); however, it will only allow you to submit one term at a time. Commented Apr 11, 2018 at 8:27
0

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');
1
  • 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
    Commented Aug 7, 2011 at 4:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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