I run a multiple author website. I want to restrict a few tags from being selected by my authors.
The only options that I found so far was techniques to replace the free tag text field with a list (similar to the category list). This is not a solution for me as I will need my authors to be able to create new tags.
Surely there must be a way to restrict specific tags from non-admins? Do you know how? Any brainstorming or proper solutions are welcome.
I think the ideal solution would be to conditionally filter the taxonomy query used in post_tags_meta_box
as it ajaxes its suggestions, and maybe even providing some error-handling if someone tried to manually type the tag you don't want them to use, but I'm not aware of a filter that could aid in pulling that off.
Expanding on Giordano's suggestion and referencing this other question, you could use something like this in functions.php
add_action('save_post', 'remove_tags_function', 10, 1); //whenever a post is saved, run the below function
function remove_tags_function( $post_id ){
if(!current_user_can('manage_options')){ // if the logged in user cannot manage options (only admin can)
$post_tags = wp_get_post_terms( $post_id, 'post_tag', array( 'fields'=>'names' ) ); //grab all assigned post tags
$pos = array_search( 'tag-to-be-deleted', $post_tags ); //check for the prohibited tag
if( false !== $pos ) { //if found
unset( $post_tags[$pos] ); //unset the tag
wp_set_post_terms ($post_id, $post_tags, 'post_tag'); //override the posts tags with all prior tags, excluding the tag we just unset
}
}//end if. If the current user CAN manage options, the above lines will be skipped, and the tag will remain
}
Non-admin users will still be able to type in and add tag-to-be-deleted
, but it will not stick to the post. Once saved, the tag will be stripped. If the user is really dedicated, they could spell it differently, or, as you saw, change the capitalization, but whatever they do it wont technically be the same tag, and you will be able to keep it pure for whatever theme purpose you need. I can't imagine a situation in which a user without admin capabilities could add the forbidden tag, but I know better than to never say never.
If you want to allow certain non-admin users to assign the forbidden tag to a post, you'll have to revise the parameter passed into line 4 if(!current_user_can('...'){
. For information on other capabilities you can pass to this conditional statement, check the Wordpress documentation of Roles & Capabilities. It's much easier to check for a capability than a role, so pick a logical capability that is restricted to the levels of user that you wish to be exempt from the tag deletion.