自定义分类法 - 根据角色或能力设置访问权限 [英] Custom taxonomy - setting access based on role or capability

查看:45
本文介绍了自定义分类法 - 根据角色或能力设置访问权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是刚刚了解 Wordpress 的自定义分类法.如何限制我的用户使用分类法的访问权限.例如,我创建了一个名为 featured 的分类法,我只希望编辑者及以上角色能够向该分类法添加帖子.

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 );

推荐答案

从帖子编辑页面中删除元框是否足够?如果是这样,请试一试这个:

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.

这篇关于自定义分类法 - 根据角色或能力设置访问权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆