map_meta_cap woes

时间:2013-07-04 作者:mantis

我已经创建了一个自定义帖子类型,并希望它的行为像一个页面。某些角色需要访问它,而不是帖子或页面。因此,我创建了我的自定义帖子类型,其中包含以下功能类型和功能:

function add_team_page_post_type($name, $args= array()) {
add_action(\'init\', function() use($name, $args) {
        $args = array(
            \'labels\' => team_page_post_type_labels( \'Team Page\' ),
            \'public\' => true,
            \'publicly_queryable\' => true,
            \'show_ui\' => true,
            \'show_in_menu\' => true,
            \'query_var\' => true,
            \'rewrite\' => true,
            \'capability_type\' => array(\'team\', \'teams\'),
            \'capabilities\' => array(
                \'publish_posts\' => \'publish_teams\',
                \'edit_posts\' => \'edit_teams\',
                \'edit_post\' => \'edit_team\',
                \'edit_others_posts\' => \'edit_others_teams\',
                \'delete_posts\' => \'delete_teams\',
                \'delete_post\' => \'delete_team\',
                \'delete_others_posts\' => \'delete_others_teams\',
                \'manage_posts\' => \'manage_teams\',
                \'read_private_posts\' => \'read_private_teams\',
                \'read_post\' => \'read_team\',
            ),
            \'map_meta_cap\'=> true,  
            \'has_archive\' => true,
            \'hierarchical\' => true,
            \'menu_position\' => null,
            \'supports\' => array(\'title\',
                \'author\',
                \'editor\',
                \'thumbnail\',
            ));

        register_post_type( $name, $args );
    });
}

add_team_page_post_type(\'team_page\', array());
并将他们分配到不同的角色,如:

add_action( \'init\', \'my_custom_capabilities\', 0);
function my_custom_capabilities(){

$role_object = get_role( \'administrator\' );
    $role_object->add_cap( \'publish_teams\');
    $role_object->add_cap( \'edit_teams\');
    $role_object->add_cap( \'edit_team\');
    $role_object->add_cap( \'edit_others_teams\');
    $role_object->add_cap( \'delete_teams\');
    $role_object->add_cap( \'delete_team\');
    $role_object->add_cap( \'delete_others_teams\');
    $role_object->add_cap( \'manage_teams\');        
    $role_object->add_cap( \'read_private_teams\');
    $role_object->add_cap( \'read_team\');
}
经过进一步研究,我发现我需要包括map_meta_cap()我喜欢的功能:

add_filter( \'map_meta_cap\', \'my_map_meta_cap\', 10, 4 );

function my_map_meta_cap( $caps, $cap, $user_id, $args ) {

/* If editing, deleting, or reading a team, get the post and post type object. */
if ( \'edit_team\' == $cap || \'delete_team\' == $cap || \'read_team\' == $cap ) {
    $post = get_post( $args[0] );
    $post_type = get_post_type_object( $post->post_type );

    /* Set an empty array for the caps. */
    $caps = array();
}

/* If editing a team, assign the required capability. */
if ( \'edit_team\' == $cap ) {
    if ( $user_id == $post->post_author )
        $caps[] = $post_type->cap->edit_posts;
    else
        $caps[] = $post_type->cap->edit_others_posts;
}

/* If deleting a team, assign the required capability. */
elseif ( \'delete_team\' == $cap ) {
    if ( $user_id == $post->post_author )
        $caps[] = $post_type->cap->delete_posts;
    else
        $caps[] = $post_type->cap->delete_others_posts;
}

/* If reading a private team, assign the required capability. */
elseif ( \'read_team\' == $cap ) {

    if ( \'private\' != $post->post_status )
        $caps[] = \'read\';
    elseif ( $user_id == $post->post_author )
        $caps[] = \'read\';
    else
        $caps[] = $post_type->cap->read_private_posts;
}

/* Return the capabilities required by the user. */
return $caps;
}
这是贾斯汀·塔洛克的一篇文章http://justintadlock.com/archives/2010/07/10/meta-capabilities-for-custom-post-types

问题是我只能查看我的cpt,不能编辑或删除它。我猜问题出在最后一个函数上,但我找不到太多关于它的文档。

1 个回复
SO网友:mantis

最后,我找到了这个插件,它在两秒钟内完成了这项工作。http://wordpress.org/plugins/map-cap/ . 我仍然很想知道它是如何完成的,但我想我可以一直研究这个插件。

结束

相关推荐

PHP致命错误:无法为wp-includes/capabilities.php中的非对象调用重载函数

我在apache日志中遇到了太多以下错误。PHP Fatal error: Cannot call overloaded function for non-object in wp-includes/capabilities.php on line 1187这是函数current\\u user\\u can($capability)的内部,第1187行如下所示:$current_user = wp_get_current_user(); 我不知道问题出在哪里?