admin管理员组

文章数量:1316679

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:

And after each save:

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;
}

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:

And after each save:

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;
}
Share Improve this question asked Mar 9, 2017 at 9:21 Krzysztof GrabaniaKrzysztof Grabania 5863 silver badges12 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 4

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;
}

本文标签: filtersChange term name only on front