I am using an editorial wordpress theme that has a frontend post editor for a user (contributor) to create a post. The editor is giving permission to the user to choose category for the article and to add tags.
I have set specific custom tags that could be assigned with the post.
How can i allow the user to only be able to add those tags and NOT any tag that he wants. For examlpe only 'exodus' 'holidays' . Dont need to create any msg for the user. Just allow to register in the database only those specific tags. Could i use the ID of the custom tags that i created somehow?
I found something very similur in this post and it works fine from the backend post editor, but from the front it doesn't.
function disallow_insert_term($term, $taxonomy) {
$user = wp_get_current_user();
if ( $taxonomy === 'post_tag' && in_array('contributor', $user->roles) ) {
return new WP_Error(
'disallow_insert_term',
__('Your role does not have permission to add terms to this taxonomy')
);
}
return $term;
}
add_filter('pre_insert_term', 'disallow_insert_term', 10, 2);
*(Note that the contributor can not publish, only send for approval an article. But when i publish the article i don't get any tags.)
Or maybe something like this but instead of removing specific tags,remove all tags except the custom ones.
function remove_tags_function( $post_id ){
$post_tags = wp_get_post_terms( $post_id, 'post_tag', array( 'fields'=>'names' ) ); //grab all assigned post tags
$pos = array_intersect( array('TAG1', 'TAG2', 'TAG3', 'ETC...'), $post_tags ); //check for the prohibited tag
if( !empty($pos) ) { //if found
$post_tags = array_diff($post_tags, $pos);
wp_set_post_terms ($post_id, $post_tags, 'post_tag'); //override the posts tags with all prior tags, excluding the tag we just unset
}
}
add_action('save_post', 'remove_tags_function', 10, 1); //whenever a post is saved, run the below function
Also the above code works fine by removing the 'TAG1', 'TAG2', 'TAG3', 'ETC...' tags, but still the tag is being created in post_tag taxonomy.
So basically i am searching for a filter that allows only specific tags to be added to the database when a user starts typing tags.
Any help would be appreciated
I am using an editorial wordpress theme that has a frontend post editor for a user (contributor) to create a post. The editor is giving permission to the user to choose category for the article and to add tags.
I have set specific custom tags that could be assigned with the post.
How can i allow the user to only be able to add those tags and NOT any tag that he wants. For examlpe only 'exodus' 'holidays' . Dont need to create any msg for the user. Just allow to register in the database only those specific tags. Could i use the ID of the custom tags that i created somehow?
I found something very similur in this post and it works fine from the backend post editor, but from the front it doesn't.
function disallow_insert_term($term, $taxonomy) {
$user = wp_get_current_user();
if ( $taxonomy === 'post_tag' && in_array('contributor', $user->roles) ) {
return new WP_Error(
'disallow_insert_term',
__('Your role does not have permission to add terms to this taxonomy')
);
}
return $term;
}
add_filter('pre_insert_term', 'disallow_insert_term', 10, 2);
*(Note that the contributor can not publish, only send for approval an article. But when i publish the article i don't get any tags.)
Or maybe something like this but instead of removing specific tags,remove all tags except the custom ones.
function remove_tags_function( $post_id ){
$post_tags = wp_get_post_terms( $post_id, 'post_tag', array( 'fields'=>'names' ) ); //grab all assigned post tags
$pos = array_intersect( array('TAG1', 'TAG2', 'TAG3', 'ETC...'), $post_tags ); //check for the prohibited tag
if( !empty($pos) ) { //if found
$post_tags = array_diff($post_tags, $pos);
wp_set_post_terms ($post_id, $post_tags, 'post_tag'); //override the posts tags with all prior tags, excluding the tag we just unset
}
}
add_action('save_post', 'remove_tags_function', 10, 1); //whenever a post is saved, run the below function
Also the above code works fine by removing the 'TAG1', 'TAG2', 'TAG3', 'ETC...' tags, but still the tag is being created in post_tag taxonomy.
So basically i am searching for a filter that allows only specific tags to be added to the database when a user starts typing tags.
Any help would be appreciated
Share Improve this question edited Mar 26, 2020 at 13:37 Honoluluman asked Mar 25, 2020 at 17:34 HonolulumanHonoluluman 351 silver badge13 bronze badges 3- After some tests i run i see that the 'save_post' action works in the front end – Honoluluman Commented Mar 25, 2020 at 22:23
- by front end editor you mean gutenberg? – BenB Commented Mar 26, 2020 at 0:49
- No it is not gutenberg. The theme uses custom editor . To be more specific its is this editor zombify.px-lab/frontend-page . You can test by using demo/demo for login and select story . – Honoluluman Commented Mar 26, 2020 at 8:55
1 Answer
Reset to default 0I found the solution to this problem here. This code doesn't allow user to create new tags from the back or front end but only use the existing custom ones that admin have set in the post_tag taxonomy.