$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>How to display warning on post editor when trying to add new term to custom taxonomy?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin8PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far