admin管理员组

文章数量:1129094

I've created some custom taxonomies using register_taxonomy, but I want to prevent new terms from being added. I noticed that there is a 'capabilities' argument available in register_taxonomy, is that what I should be using and if so, how would I use it?

Here's some of my code, I'm using a plugin I created to add the taxonomies (hence the public static function part). Is there an easy way to prevent new terms being created for my taxonomy?

Thanks

Osu

public static function register_directory_styles_taxonomy()
    {
        $labels = array(
            'name'                      => 'Music styles',
            'singular_name'             => 'Music style',
            'search_items'              => 'Search music styles',
            'all_items'                 => 'All music styles',
            'parent_item'               => 'Parent music style',
            'edit_item'                 => 'Edit music style',
            'update_item'               => 'Update music style',
            'add_new_item'              => 'Add new music style',
            'new_item_name'             => 'New music style',
            'choose_from_most_used'     => 'Choose from most used music styles'
        );

        $args = array(
            'hierarchical'  => true,
            'labels'        => $labels,
            'rewrite'       => false
            // 'show_ui'        => false
        );

        register_taxonomy( 'ibmstyles', 'ibmdirectory', $args );
    }

I've created some custom taxonomies using register_taxonomy, but I want to prevent new terms from being added. I noticed that there is a 'capabilities' argument available in register_taxonomy, is that what I should be using and if so, how would I use it?

Here's some of my code, I'm using a plugin I created to add the taxonomies (hence the public static function part). Is there an easy way to prevent new terms being created for my taxonomy?

Thanks

Osu

public static function register_directory_styles_taxonomy()
    {
        $labels = array(
            'name'                      => 'Music styles',
            'singular_name'             => 'Music style',
            'search_items'              => 'Search music styles',
            'all_items'                 => 'All music styles',
            'parent_item'               => 'Parent music style',
            'edit_item'                 => 'Edit music style',
            'update_item'               => 'Update music style',
            'add_new_item'              => 'Add new music style',
            'new_item_name'             => 'New music style',
            'choose_from_most_used'     => 'Choose from most used music styles'
        );

        $args = array(
            'hierarchical'  => true,
            'labels'        => $labels,
            'rewrite'       => false
            // 'show_ui'        => false
        );

        register_taxonomy( 'ibmstyles', 'ibmdirectory', $args );
    }
Share Improve this question asked Sep 3, 2013 at 9:45 OsuOsu 1,4526 gold badges25 silver badges44 bronze badges 2
  • Why do you comment out 'show_ui' => false? – gmazzap Commented Sep 3, 2013 at 11:03
  • 2 You are on the right track with capabilities. You can take a additional look at codex.wordpress.org/Function_Reference/register_post_type, because there is a little bit mor information as on the codex.wordpress.org/Function_Reference/register_taxonomy. Besides that, this: wordpress.stackexchange.com/questions/16588/… and this: wordpress.stackexchange.com/questions/21411/… might help you. At the latter especially the link in the answer. – Nicolai Grossherr Commented Sep 3, 2013 at 11:35
Add a comment  | 

2 Answers 2

Reset to default 24

You can block addition of new terms with a filter on pre_insert_term. The source is helpful in working out what you can do.

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

It would seem the easiest way to do this has been answered here.

The solution was modifying the taxonomy's capabilities array:

'capabilities' => array(
    'manage_terms' => '',
    'edit_terms' => '',
    'delete_terms' => '',
    'assign_terms' => 'edit_posts'
),

本文标签: How to prevent new terms being added to a custom taxonomy