11

I want a certain role to be able to add new tags, but other users should just be able to use the text bar to search for existing tags.

I know I can hide the tag bar in the sidebar, but I don't want that. I just want it to say "Sorry, this tag does not exist" or something when someone enters a tag that doesn't exist.

Is there a plugin or a function that will do that for me?

1 Answer 1

12

You can hook onto pre_insert_term, check the taxonomy and whether or not the user has the specified role as follows:

function disallow_insert_term($term, $taxonomy) {

    $user = wp_get_current_user();

    if ( $taxonomy === 'post_tag' && in_array('somerole', $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);

This will prevent the user inserting new terms but allow them to search and add existing terms to a post.

Beaware that when on the post edit screen a user can enter in a term name that does not exist and press enter or click the add button which will add the term to the DOM however at this point the term is not added to the database until the user publishes or updates the post at which point the term will be disallowed.

11
  • Hi, thanks for the help :) I have tried this, but it didn't seem to do anything. I'll be honest and say it's probably my own fault. I copy pasted your code in my functions and of course changed the 'somerole' bit. Adding tags is still possible for that role. Jul 6, 2015 at 20:14
  • What is the user role you are trying to prevent? Also are you trying to prevent tags for the correct taxonomy, in this case post_tag and not some other taxonomy like categories or some custom taxonomy?
    – Adam
    Jul 7, 2015 at 3:39
  • I'm trying to prevent the editor role from doing it at the moment. And it's the standard taxonomy (so that should be post_tag), I haven't created anything else yet as i'm still testing. Jul 7, 2015 at 6:26
  • Time to start debugging then. In wp-config.php set the following constants, define('WP_DEBUG', true);, define('WP_DEBUG_LOG', true);, and define('WP_DEBUG_DISPLAY', false); then in the function snippet above, log a call to error_log($taxonomy); and another call to error_log(implode(', ', $user->roles)); then add a tag then update the post, this will then create a debug.log file in the wp-content directory. You need to work out whether or not this function is being called and whether or not it is entering the conditional statement correctly because this callback works for me.
    – Adam
    Jul 7, 2015 at 11:34
  • See pastebin.com/nX9ZLubH
    – Adam
    Jul 7, 2015 at 11:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.