Custom taxonomy - setting access based on role or

2019-02-15 23:29发布

I am only just learning about custom taxonomies for Wordpress. How is it possible to restrict access for my users to use a taxonomy. For instance, I have created a taxonomy named featured and I only want Editors and above roles to be able to add posts to this taxonomy.

How do I set the access level? Either based on user role or capability, both works for me.

Here is the code that I use for my taxonomy:

function add_custom_taxonomies() {
    // Add new "Featured" taxonomy to Posts
    register_taxonomy('featured', 'post', array(
        // Hierarchical taxonomy (like categories)
        'hierarchical' => true,
        // This array of options controls the labels displayed in the WordPress Admin UI
        'labels' => array(
            'name' => _x( 'Featured', 'taxonomy general name' ),
            'singular_name' => _x( 'Featured', 'taxonomy singular name' ),
            'search_items' =>  __( 'Search Featured' ),
            'all_items' => __( 'All Featured' ),
            'parent_item' => __( 'Parent Featured' ),
            'parent_item_colon' => __( 'Parent Featured:' ),
            'edit_item' => __( 'Edit Featured' ),
            'update_item' => __( 'Update Featured' ),
            'add_new_item' => __( 'Add New Featured' ),
            'new_item_name' => __( 'New Featured Name' ),
            'menu_name' => __( 'Featured' ),
        ),
        // Control the slugs used for this taxonomy
        'rewrite' => array(
            'slug' => 'featured', // This controls the base slug that will display before each term
            'with_front' => false, // Don't display the category base before "/locations/"
            'hierarchical' => true // This will allow URL's like "/locations/boston/cambridge/"
        ),
    ));
}
add_action( 'init', 'add_custom_taxonomies', 0 );

1条回答
放荡不羁爱自由
2楼-- · 2019-02-15 23:39

Would it be sufficient to remove the metabox from the Post Edit page? If so, give this a whirl:

function remove_featured_meta() {
   if (!current_user_can('moderate_comments')){
       remove_meta_box( 'featureddiv', 'post', 'side' );
   }
}

add_action( 'admin_menu' , 'remove_featured_meta' );

You can fill in the conditional statement with whichever appropriate capability corresponds to the user role to which you'd like to limit access to the taxonomy.

查看更多
登录 后发表回答