How to display warning on post editor when trying to add new term to custom taxonomy?

admin2025-06-06  9

i have a function i use to stop users from adding new terms to custom taxonomies, this is the code:

add_action( 'pre_insert_term', 'prevent_terms', 1, 2 );
function prevent_terms ( $term, $taxonomy ) {
    if ( 'language' === $taxonomy) {
      return new WP_Error( 'term_addition_blocked', __( 'You cannot add terms to this taxonomy' ) );
    }

    return $term;
}

This works fine when you are on the custom taxonomy page (/wp-admin/edit-tags.php?taxonomy=language) Because you see the warning.

BUT when you try to add a new term by saving a post, you get no warning, the term simply don't get saved. What i need is add a warning to this function so the person saving a post knows he cannot add new terms to this taxonomy.

Any way to achieve this? Thanks.

i have a function i use to stop users from adding new terms to custom taxonomies, this is the code:

add_action( 'pre_insert_term', 'prevent_terms', 1, 2 );
function prevent_terms ( $term, $taxonomy ) {
    if ( 'language' === $taxonomy) {
      return new WP_Error( 'term_addition_blocked', __( 'You cannot add terms to this taxonomy' ) );
    }

    return $term;
}

This works fine when you are on the custom taxonomy page (/wp-admin/edit-tags.php?taxonomy=language) Because you see the warning.

BUT when you try to add a new term by saving a post, you get no warning, the term simply don't get saved. What i need is add a warning to this function so the person saving a post knows he cannot add new terms to this taxonomy.

Any way to achieve this? Thanks.

Share Improve this question asked Sep 4, 2016 at 16:53 Michael RogersMichael Rogers 5498 silver badges37 bronze badges 8
  • you can try to hide the taxonomy metabox on the edit page and add a custom metabox where we can only choose the existing terms – mmm Commented Sep 4, 2016 at 17:05
  • Why do show that box if it shouldn't be used? The correct way is setting the proper capabilities when you register the taxonomy. – fuxia Commented Sep 4, 2016 at 17:22
  • @mmm that would work, i wouldn't know how to do it though. – Michael Rogers Commented Sep 4, 2016 at 18:11
  • @toscho You are confused. The box is there for users to pick from the hundred of available terms. Not to add new ones that are often misspelled, duplicated or abbreviations. – Michael Rogers Commented Sep 4, 2016 at 18:12
  • I am talking about the Add new box, not the Select box. You can avoid showing both by registering the taxonomy correctly. – fuxia Commented Sep 4, 2016 at 19:17
 |  Show 3 more comments

1 Answer 1

Reset to default 7 +50

Whilst this doesn't show a warning as you are asking for, you could always hide the "add new" link using the admin_head action:

function yourprefix_admin_head() {
   echo '<style>
   #language-add-toggle {
      display: none;
   }
   </style>';
}
add_action('admin_head', 'yourprefix_admin_head');

The element ID is the taxonomy name followed by -add-toggle.

This is enough for most cases, unless you think your users are a bit devious. Hope it helps.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749217897a317350.html

最新回复(0)