Change term name only on front

Question

I am trying to change term name for users based on custom select. In term meta I am storing ID of select option and based on this ID I am looking for proper name in another taxonomy. Now I want to add this portion of text to each term name. The only filters I can find is get_term (or get_{$taxonomy}) and get_terms for more items.

Here is the problem: after adding some text to $term->name in any of this filters it is showing in edit term screen in name input. This is nothing weird, because I filtered name so it is showing in every place, but how can I prevent this behavior on admin screen? This is problematic, because this text is option in select, so after each save next name will be appended to my term name.

Problem on image:

enter image description here

And after each save:

enter image description here

This is my code:

add_filter('get_term', 'filter_term_name', 10, 2);

function filter_term_name($term, $taxonomy) {
    if ($taxonomy == 'my-taxonomy') {
        // numeric value of term in another taxonomy
        $meta_value = get_term_meta($term->term_id, '_my_meta_name', true);

        if ($meta_value) {
            // get name of meta value from another taxonomy
            $value = get_term($meta_value, 'another-taxonomy');

            if ($value) {
                $term->name = sprintf('%s, %s', $value->name, $term->name);
            }
        }
    }

    return $term;
}
0
, , Krzysztof Grabania 7 years 2017-03-09T03:21:11-05:00 0 Answers 66 views 0

Leave an answer

Browse
Browse