This PHP code snippet prevents users from creating new tags directly from the post editor in WordPress, helping maintain a clean and organized tag structure.
By intercepting attempts to create new tags via the REST API, it ensures that users can only use existing tags when writing or editing posts.
This approach encourages better tag management by requiring new tags to be created intentionally through the Tags management page, reducing tag proliferation and improving overall site organization.
/** * Prevent users from creating new tags via REST API */ function prevent_new_tags_creation($response, $handler, $request) { if ($request->get_route() === '/wp/v2/tags' && $request->get_method() === 'POST') { return new WP_Error( 'rest_cannot_create_tag', __('Sorry, you are not allowed to create new tags. Please use existing tags only.'), array('status' => 403) ); } return $response; } add_filter('rest_pre_dispatch', 'prevent_new_tags_creation', 10, 3);