CURRENT_USER_CAN()返回FALSE,但调试显示为TRUE

时间:2015-09-03 作者:Coloumbo

出身背景

我已经创建了一个必须使用的插件,在这里我保留了常规功能。比如register_post_type()。

在这里,我创建了几种自定义帖子类型。在几个非常好的帖子的帮助下,我设法掌握了定制帖子类型的能力(我想)。

////////////////
//
// Create Custom Post Types
//
////////////////

add_action( \'init\', \'create_post_type\' );

    function create_post_type() {
    register_post_type( \'pms\',
        array(
          \'labels\' => array(
            \'name\' => __( \'Personal Mission Statements\' ),
            \'singular_name\' => __( \'Personal Mission Statement\' )
          ),
          \'public\' => true,
          \'has_archive\' => true,
          \'menu_position\' => 2,
          \'menu_icon\' => \'dashicons-heart\',
          \'capability_type\' => array(\'pms\',\'pmss\'),
          \'map_meta_cap\' => true,
          \'supports\'           => array( \'title\', \'editor\', \'author\' )

        )
      );
      flush_rewrite_rules();
}

////////////////
//
// Create Custom Roles
//
////////////////


/////////
//Add new roles
////////

//Can\'t create new post and only edit their own
$capabilities_employee = array (
    \'edit_others_pages\' => true,
    \'edit_others_posts\' => true,
    \'edit_pages\' => true,
    \'edit_posts\' => true,
    \'edit_private_posts\' => true,
    \'edit_published_posts\' => true,
    \'list_users\' => true,
    \'manage_categories\' => true,
    \'publish_posts\' => true,
    \'read\' => true,
    \'upload_files\' => true,
    \'manage_categories\' => true,
    );

//Create additional Roles

function add_roles () {
    add_role( \'employee\', \'Employee\', $capabilities_employee );
}

add_action( \'admin_init\', \'add_roles\');

////////
//Specify Capabilities Custom Post Types
////////


//Add capabilities to the other Post Types (need added).
//The remove a capabilities it is NOT enough to remove the line, you need to add remove_cap()

function add_theme_caps() {

    // gets the candidate role
    $employees = get_role( \'employee\' );

    $employees->add_cap( \'read\' );

    $employees->add_cap( \'edit_pms\' ); 
    $employees->add_cap( \'read_pms\' ); 
    $employees->add_cap( \'edit_pmss\' );
    $employees->add_cap( \'publish_pmss\' ); 
    $employees->add_cap( \'edit_published_pmss\' );
}
add_action( \'admin_init\', \'add_theme_caps\');
问题在编辑自定义帖子“pms”之前,我会检查当前的\\u用户\\u can(“编辑\\u pms”)

1) 默认角色“editor”返回true 2)自定义角色“employee”(如上)返回FALSE

使用$GLOBALS[\'wp\\u post\\u types\']调试“pms”自定义post类型上的功能,并使用get\\u userdata()调试具有employee角色的用户的功能。它们都具有正确的功能。

问题上述任何想法方式(2)返回FALSE?

来源

https://codex.wordpress.org/Function_Reference/register_post_typehttp://justintadlock.com/archives/2010/07/10/meta-capabilities-for-custom-post-types

1 个回复
SO网友:kaiser

这个register_post_type() 函数将post类型名称作为的参数map_meta_cap, 其中默认值为post. 看看get_post_type_capabilities() 以获得更深入的见解。这应该有助于您理解它的含义:

function get_post_type_capabilities( $args ) {
    if ( ! is_array( $args->capability_type ) )
        $args->capability_type = array( $args->capability_type, $args->capability_type . \'s\' );

    // Singular base for meta capabilities, plural base for primitive capabilities.
    list( $singular_base, $plural_base ) = $args->capability_type;

    $default_capabilities = array(
        // Meta capabilities
        \'edit_post\'          => \'edit_\'         . $singular_base,
        \'read_post\'          => \'read_\'         . $singular_base,
        \'delete_post\'        => \'delete_\'       . $singular_base,
        // Primitive capabilities used outside of map_meta_cap():
        \'edit_posts\'         => \'edit_\'         . $plural_base,
        \'edit_others_posts\'  => \'edit_others_\'  . $plural_base,
        \'publish_posts\'      => \'publish_\'      . $plural_base,
        \'read_private_posts\' => \'read_private_\' . $plural_base,
    );

    // Primitive capabilities used within map_meta_cap():
    if ( $args->map_meta_cap ) {
        $default_capabilities_for_mapping = array(
            \'read\'                   => \'read\',
            \'delete_posts\'           => \'delete_\'           . $plural_base,
            \'delete_private_posts\'   => \'delete_private_\'   . $plural_base,
            \'delete_published_posts\' => \'delete_published_\' . $plural_base,
            \'delete_others_posts\'    => \'delete_others_\'    . $plural_base,
            \'edit_private_posts\'     => \'edit_private_\'     . $plural_base,
            \'edit_published_posts\'   => \'edit_published_\'   . $plural_base,
        );
        $default_capabilities = array_merge( $default_capabilities, $default_capabilities_for_mapping );
    }

    $capabilities = array_merge( $default_capabilities, $args->capabilities );

    // Post creation capability simply maps to edit_posts by default:
    if ( ! isset( $capabilities[\'create_posts\'] ) )
        $capabilities[\'create_posts\'] = $capabilities[\'edit_posts\'];

    // Remember meta capabilities for future reference.
    if ( $args->map_meta_cap )
        _post_type_meta_capabilities( $capabilities );

    return (object) $capabilities;
}
更好的调试方法是使用以下挂钩:

do_action( \'registered_post_type\', $post_type, $args );
像这样使用它(它在注册后运行):

add_action( \'registered_post_type\', function( $cpt, $args )
{
    $cpt === \'your_cpt_name\' && var_dump(
        $args->capability_type,
        $args->cap // result of get_post_type_capabilities()
    );
}, 10, 2 );
更换your_cpt_name 使用您在注册期间用作第一个参数的实际名称

还要记住,对于大多数用例,完全没有必要对照自定义功能进行检查。这些都是很难维持的。我更喜欢对照post类型和默认功能进行检查。

相关推荐

未定义的偏移量:1067行的>[...]/wp-includes/capabilities.php中的0

嘿,我在我的localhost设置中得到了这个错误消息,但只有在启用Genesis框架的情况下;WordPress二十一行。当我想创建一个新帖子时,就会发生这种情况。如果我刷新页面,错误会重复,但帖子本身会被创建,一切似乎都很好。有人知道这是什么原因吗?Notice: Undefined offset: 0 in /var/www/secret/htdocs/wp-includes/capabilities.php on line 1067 Notice: Undefined offset: 0