WordPress.org

Ready to get started?Download WordPress

Forums

Comma in tag (6 posts)

  1. Jay
    Member
    Posted 2 years ago #

    I have looked around for this, but have not found anything recent. I need a comma in my tag.

    Example:
    city, state

    can someone point me in the right direction?

    Thanks

  2. Jay
    Member
    Posted 2 years ago #

    Just to clarify, I am using get_tag and by default, individual tags are separated by a comma. So I have to do "city state" as one tag. I need "city, state" as one tag.

  3. foobored
    Member
    Posted 2 years ago #

    And here you go:

    Save your tags e.g. with "City--State", write a filter, hook it up to get_term, get_terms and get_the_terms and simply replace "--" before output with ", ".

    // filter for tags with comma
    //  replace '--' with ', ' in the output - allow tags with comma this way
    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);

    More detailed description here http://blog.foobored.com/all/wordpress-tags-with-commas/

  4. Jay
    Member
    Posted 2 years ago #

    Excellent. Great work, thanks

  5. foobored
    Member
    Posted 2 years ago #

    One minor update:

    You have to put an check routine if currently the admin interface is shown or not:

    if(!is_admin()){
       // here goes all the code
    }

    This is necessary in order to be actually able to save the tags correctly in the admin interface.

  6. Jay
    Member
    Posted 2 years ago #

    Thanks for the update.
    Cheers!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags