4

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.

  • 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 Sep 4 '16 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 Sep 4 '16 at 17:22
  • @mmm that would work, i wouldn't know how to do it though. – Michael Rogers Sep 4 '16 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 Sep 4 '16 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 Sep 4 '16 at 19:17
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.

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.