Restrict tag creation
✓ Published0🌍 Public
KKopylovVitalik
Last edited Sep 7, 2020
Created on Sep 7, 2020
This example shows a WordPress snippet that restricts editors from creating new post tags. It hooks into the `pre_insert_term` action, which fires before a term is inserted, and checks the current user’s role against the taxonomy. If the taxonomy is `post_tag` and the user has the `editor` role, it returns a `WP_Error` object, preventing the tag creation and displaying a custom permission message. The code uses `wp_get_current_user()` to access the user’s roles array and the `add_action` function with a priority of 10 and two accepted arguments.
AI-generated description