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

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.

1 Answer
1

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.

Leave a Comment