功能和自定义帖子类型


30

我有一个自定义帖子类型,我想限制对某些角色的访问,但是,我已经使用该自定义帖子类型添加了内容,现在必须对其进行限制。capability_type是“ post”

'capability_type' => 'post'

但是,当内容显示在后端时,哪个很好,但是现在,一旦添加任何功能,内容就会从后端消失?

我尝试自定义功能类型以包括复数定义来构造自己的功能类型,但是一旦删除或更改功能类型,它就消失了!

完整代码:

add_action( 'init', 'register_cpt_gallery' );

function register_cpt_gallery() {
$labels = array( 
    'name' => _x( 'Galleries', 'gallery' ),
    'singular_name' => _x( 'Gallery', 'gallery' ),
    'add_new' => _x( 'Add New', 'gallery' ),
    'add_new_item' => _x( 'Add New Gallery', 'gallery' ),
    'edit_item' => _x( 'Edit Gallery', 'gallery' ),
    'new_item' => _x( 'New Gallery', 'gallery' ),
    'view_item' => _x( 'View Gallery', 'gallery' ),
    'search_items' => _x( 'Search Galleries', 'gallery' ),
    'not_found' => _x( 'No galleries found', 'gallery' ),
    'not_found_in_trash' => _x( 'No galleries found in Trash', 'gallery' ),
    'parent_item_colon' => _x( 'Parent Gallery:', 'gallery' ),
    'menu_name' => _x( 'Galleries', 'gallery' ),
);

$args = array( 
    'labels' => $labels,
    'hierarchical' => true,
    'description' => 'Image galleries for teachers classes',
    'supports' => array( 'title', 'editor', 'author'),

    'public' => true,
    'show_ui' => true,
    'show_in_menu' => true,

    'menu_icon' => get_bloginfo('template_url') . '/images/imagegallery.png',
    'show_in_nav_menus' => true,
    'publicly_queryable' => true,
    'exclude_from_search' => false,
    'has_archive' => true,
    'query_var' => true,
    'can_export' => true,
    'rewrite' => true,
    'capability_type' => 'post',
    'capabilities' => array(
        'edit_post' => 'edit_gallery',
        'edit_posts' => 'edit_galleries',
        'edit_others_posts' => 'edit_other_galleries',
        'publish_posts' => 'publish_galleries',
        'read_post' => 'read_gallery',
        'read_private_posts' => 'read_private_galleries',
        'delete_post' => 'delete_gallery'
    )
);

register_post_type( 'gallery', $args );
}

我还用一种全新的自定义帖子类型测试了这一点,无论功能类型如何,我都会遇到相同的问题,例如即使删除它并添加我的自定义帖子也是如此:

'capability_type' => array('movie','movies');

Answers:


40

Magicroundabout快速聊天后,后者指出了贾斯汀· 塔德洛克Justin Tadlock)的有用资源,结果表明,除非您对角色使用add_cap(例如,以下自定义帖子类型),否则自定义帖子类型的功能实际上并不存在。

add_action( 'init', 'register_cpt_gallery' );

function register_cpt_gallery() {
$labels = array( 
    'name' => _x( 'Galleries', 'gallery' ),
    'singular_name' => _x( 'Gallery', 'gallery' ),
    'add_new' => _x( 'Add New', 'gallery' ),
    'add_new_item' => _x( 'Add New Gallery', 'gallery' ),
    'edit_item' => _x( 'Edit Gallery', 'gallery' ),
    'new_item' => _x( 'New Gallery', 'gallery' ),
    'view_item' => _x( 'View Gallery', 'gallery' ),
    'search_items' => _x( 'Search Galleries', 'gallery' ),
    'not_found' => _x( 'No galleries found', 'gallery' ),
    'not_found_in_trash' => _x( 'No galleries found in Trash', 'gallery' ),
    'parent_item_colon' => _x( 'Parent Gallery:', 'gallery' ),
    'menu_name' => _x( 'Galleries', 'gallery' ),
);

$args = array( 
    'labels' => $labels,
    'hierarchical' => true,
    'description' => 'Image galleries for teachers classes',
    'supports' => array( 'title', 'editor', 'author'),
    'public' => true,
    'show_ui' => true,
    'show_in_menu' => true,
    'menu_icon' => get_bloginfo('template_url') . '/images/imagegallery.png',
    'show_in_nav_menus' => true,
    'publicly_queryable' => true,
    'exclude_from_search' => false,
    'has_archive' => true,
    'query_var' => true,
    'can_export' => true,
    'rewrite' => true,
    'capabilities' => array(
        'edit_post' => 'edit_gallery',
        'edit_posts' => 'edit_galleries',
        'edit_others_posts' => 'edit_other_galleries',
        'publish_posts' => 'publish_galleries',
        'read_post' => 'read_gallery',
        'read_private_posts' => 'read_private_galleries',
        'delete_post' => 'delete_gallery'
    ),
    // as pointed out by iEmanuele, adding map_meta_cap will map the meta correctly 
    'map_meta_cap' => true
);

register_post_type( 'gallery', $args );
}

应该在角色中添加其他功能,以使权限在后端中实际起作用,包括“管理员”,例如:

function add_theme_caps() {
    // gets the administrator role
    $admins = get_role( 'administrator' );

    $admins->add_cap( 'edit_gallery' ); 
    $admins->add_cap( 'edit_galleries' ); 
    $admins->add_cap( 'edit_other_galleries' ); 
    $admins->add_cap( 'publish_galleries' ); 
    $admins->add_cap( 'read_gallery' ); 
    $admins->add_cap( 'read_private_galleries' ); 
    $admins->add_cap( 'delete_gallery' ); 
}
add_action( 'admin_init', 'add_theme_caps');

我希望这对其他人有用。


11
add_theme_caps()应该只调用一次,而不是每次加载管理页面时调用。最好将其switch_theme用作主题激活或register_activation_hook插件激活的挂钩。
d79

真好!如果它是一个完全自定义/唯一的站点,我喜欢使用wp cli来添加功能,因为这是一个只需发生一次的操作。
squarecandy


1

恕我直言,您永远不会映射自己的能力。确保使用地图meta cap插件来这样做。 http://codex.wordpress.org/Function_Reference/map_meta_cap

我花了几天的时间尝试使用代码手动映射自定义上限。只需安装该插件,绘制您的帽子,然后在工作后停用即可。如果创建自定义角色,则需要成员插件。

我测试以确保我的角色具有这些能力的方式(有时您发誓但实际上没有)使调试页面如下:

    if( !function_exists( 'current_user_has_role' ) ){
        function current_user_has_role( $role ){
            $current_user = new WP_User( wp_get_current_user()->ID );
            $user_roles = $current_user->roles;
            $is_or_not = in_array( $role, $user_roles );
            return $is_or_not;
        }
    }

这将向您显示实际上具有的功能。


-1

对于自定义帖子类型,我建议使用钩子:

add_action( 'registered_post_type', 'your_func', 10, 2 );

相反,我建议使用:

add_filter( 'register_post_type_args', 'your_func', 10, 2 );
function your_func( $args, $name ) 
{
   if ( $name == "your_custom_post_name" ) 
   ...
}

该建议是一个好建议,但不能回答问题。
Aurovrata
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.