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
1 Answer
Reset to default 4You 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
版权声明:本文标题:filters - Change term name only on front 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742006375a2412038.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论