0

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

You can detect if you are in the frontend using the function is_admin(), like so:

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

function filter_term_name($term, $taxonomy) {
    // return the term untuched if you are in the admin
    if( is_admin() ) {
        return $term;
    }
    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;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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