categories - Is it possible to disable certain user roles from creating tags?

admin2025-01-07  3

As a site admin, I'd like to have control over things.

I'd like to prevent my contributors and authors ( who generate content for me ) from creating new tags. I don't want that tag taxonomy to turn into a zoo! Especially when you know that wordpress creates separate terms for apple and Apple! I give them a while list ( of 1000 tags that I want them to cover ) and that's it. They cannot come up with the 1001st.

How do I achieve that so that my authors just pick from what I give them?

As a site admin, I'd like to have control over things.

I'd like to prevent my contributors and authors ( who generate content for me ) from creating new tags. I don't want that tag taxonomy to turn into a zoo! Especially when you know that wordpress creates separate terms for apple and Apple! I give them a while list ( of 1000 tags that I want them to cover ) and that's it. They cannot come up with the 1001st.

How do I achieve that so that my authors just pick from what I give them?

Share Improve this question asked Apr 21, 2012 at 5:46 Average JoeAverage Joe 1,8894 gold badges24 silver badges41 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 1

This Wordpress Answers thread has exactly the information you are looking for - Plugin to restrict non-admin user to existing tags

Simply change "administrator" to whatever you need.

This is the most solid answer to this, if anyone ever comes to this post again.

This this in your functions.php file.

add_action( 'pre_insert_term', function ( $term, $taxonomy )
{
    return ( 'yourtax' === $taxonomy )
        ? new WP_Error( 'term_addition_blocked', __( 'You are unauthorized to add new terms.' ) )
        : $term;
}, 0, 2 );

I hope this helps anyone in the future.

Paul's answer is great as it also prevents terms being created via the REST API (as in the case of the block editor). For a bit more advanced version you can do:

add_action( 'pre_insert_term', function ( $term, $taxonomy )
{
    // get current taxonomy settings
    $tax = get_taxonomies( array(), 'object' )[ $taxonomy ] ?? false;

    // get defined permission to manage terms
    $cap = $tax->cap->manage_terms ?? false;

    return ( $cap && ! current_user_can( $cap ) )
        ? new WP_Error( 'term_addition_blocked', __( 'You are not authorized to add new terms.' ) )
        : $term;
}, 0, 2 );

This will block terms from being added for all taxonomies unless you have the capability assigned when registering the terms, which defaults to 'manage_categories'. By default only Editors and up have this capability, but you can adjust roles or even assign a different capability to your custom taxonomy when registering or modify the capabilities of builtin taxonomies.

assign_terms defaults to the 'edit_posts', so by default Contributors and up will still be able to add terms to posts, but this will prevent them from creating new ones.

Check this answer to your solution. I used remove_meta_box() WordPress internal function to solve similiar problem.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736261101a715.html

最新回复(0)