Custom taxonomy - setting access based on role or capability

倾然丶 夕夏残阳落幕 提交于 2019-12-30 07:47:53

问题


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:


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.



来源:https://stackoverflow.com/questions/12464220/custom-taxonomy-setting-access-based-on-role-or-capability

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!