克隆帖子类型的角色功能

时间:2014-08-06 17:09:59

标签: wordpress custom-post-type role capability

我在插件中注册了自定义帖子类型(“播放列表”)。 我已经阅读了很多关于角色和功能的内容,但这很难完全理解...... 我希望通过角色来实现post类型的CLONE功能: 在我的情况下,我想从“post”(按角色)获取功能,并将它们分配给我的自定义帖子类型(“播放列表”)

我不想像下面那样使用“capability_type”,因为我不想将“post”功能分配给我的“播放列表”帖子类型,我希望它具有自己的功能但具有相同的默认值对于每个角色。

function register_post_type() {
    $args = array( 
        ...
        'capability_type' => 'post', //I think I don't need this
        ...
    );
    register_post_type( 'playlist', $args );
)

所以我想我需要更像这样的东西:

function register_post_type() {
    $args = array( 
        ...
        'capability_type'     => 'playlist',
        'map_meta_cap'        => true,
        'capabilities' => array(
            'edit_post'              => 'edit_playlist',
            'read_post'              => 'read_playlist',
            'delete_post'            => 'delete_playlist',
            'create_posts'           => 'create_playlists',
            'edit_posts'             => 'edit_playlists',
            'edit_others_posts'      => 'manage_playlists',
            'publish_posts'          => 'manage_playlists',
            'read_private_posts'     => 'read',
            'read'                   => 'read',
            'delete_posts'           => 'manage_playlists',
            'delete_private_posts'   => 'manage_playlists',
            'delete_published_posts' => 'manage_playlists',
            'delete_others_posts'    => 'manage_playlists',
            'edit_private_posts'     => 'edit_playlists',
            'edit_published_posts'   => 'edit_playlists'
        ),
        ...
    );
    register_post_type( 'playlist', $args );
)

但之后我需要运行一个函数,根据“post”功能将这些功能分配给每个角色。 明白我的意思?

你知道如何实现这个目标吗?

谢谢!

PS:有用的链接:http://justintadlock.com/archives/2013/09/13/register-post-type-cheat-sheet

1 个答案:

答案 0 :(得分:0)

这是我最后做的 这不是问题的答案(克隆一个角色),但它适用于我需要的东西。

    function register_post_type() {
        ...
        'capability_type' => 'playlist',
        'map_meta_cap'        => true,
        'capabilities' => array(

            // meta caps (don't assign these to roles)
            'edit_post'              => 'edit_playlist',
            'read_post'              => 'read_playlist',
            'delete_post'            => 'delete_playlist',

            // primitive/meta caps
            'create_posts'           => 'create_playlists',

            // primitive caps used outside of map_meta_cap()
            'edit_posts'             => 'edit_playlists',
            'edit_others_posts'      => 'manage_playlists',
            'publish_posts'          => 'manage_playlists',
            'read_private_posts'     => 'read',

            // primitive caps used inside of map_meta_cap()
            'read'                   => 'read',
            'delete_posts'           => 'manage_playlists',
            'delete_private_posts'   => 'manage_playlists',
            'delete_published_posts' => 'manage_playlists',
            'delete_others_posts'    => 'manage_playlists',
            'edit_private_posts'     => 'edit_playlists',
            'edit_published_posts'   => 'edit_playlists'
            ),
        ...
        );

        register_post_type( $this->post_type, $args );
    }

    function set_roles_capabilities(){

        global $wp_roles;
        if ( ! isset( $wp_roles ) ) $wp_roles = new WP_Roles();

        //create a new role, based on the subscriber role 
        $role_name = 'playlist_author';
        $subscriber = $wp_roles->get_role('subscriber');
        $wp_roles->add_role($role_name,__('Playlist Author','xspfpl'), $subscriber->capabilities);

        //list of custom capabilities and which role should get it
        $wiki_caps=array(
            'manage_playlists'=>array('administrator','editor'),
            'edit_playlists'=>array('administrator','editor',$role_name),
            'create_playlists'=>array('administrator','editor',$role_name),
        );

        foreach ($wiki_caps as $wiki_cap=>$roles){
            foreach ($roles as $role){
                $wp_roles->add_cap( $role, $wiki_cap );
            }
        }

    }

    register_activation_hook( __FILE__ , 'set_roles_capabilities' );//roles & capabilities
    add_action( 'init', 'register_post_type');