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 .png
And it shows up fine. But anything added from the posts sidebar does not show up ok here:
image .png
There is some discussion on it here: 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.
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/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 edited Aug 5, 2011 at 23:09 cwd asked Aug 5, 2011 at 8:53 cwdcwd 1,8625 gold badges27 silver badges44 bronze badges 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 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
Reset to default 9No 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/all/wordpress-tags-with-commas/
Greets, Andi
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.
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.
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 );
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');