功能和自定义发布类型

时间:2013-07-30 作者:erichmond

我有一个自定义帖子类型,我想限制对某些角色的访问。但是,我已经使用自定义帖子类型添加了内容,现在我必须限制它们。能力类型为“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\');

4 个回复
最合适的回答,由SO网友:erichmond 整理而成

与快速聊天后Magicroundabout 世卫组织指出了Justin Tadlock, 事实证明,除非对角色使用add\\u cap,否则自定义帖子类型的功能实际上并不存在,例如,对于以下自定义帖子类型:

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

function register_cpt_gallery() {
$labels = array( 
    \'name\' => __( \'Galleries\', \'gallery\' ),
    \'singular_name\' => __( \'Gallery\', \'gallery\' ),
    \'add_new\' => __( \'Add New\', \'gallery\' ),
    \'add_new_item\' => __( \'Add New Gallery\', \'gallery\' ),
    \'edit_item\' => __( \'Edit Gallery\', \'gallery\' ),
    \'new_item\' => __( \'New Gallery\', \'gallery\' ),
    \'view_item\' => __( \'View Gallery\', \'gallery\' ),
    \'search_items\' => __( \'Search Galleries\', \'gallery\' ),
    \'not_found\' => __( \'No galleries found\', \'gallery\' ),
    \'not_found_in_trash\' => __( \'No galleries found in Trash\', \'gallery\' ),
    \'parent_item_colon\' => __( \'Parent Gallery:\', \'gallery\' ),
    \'menu_name\' => __( \'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\');
我希望这对其他人有用。

此外_x() translation函数要求第二个参数为string $context, 这是一个简短的描述string $domain. 如果不提供说明,请使用__() 而是具有string $domain 作为第二个参数。

SO网友:iEmanuele

添加:

map_meta_cap => true
到您的$args数组。看here, 更多信息。希望有帮助!

SO网友:Ben Racicot

我认为你从来没有映射过自己的能力。确保使用map meta cap插件来执行此操作。http://codex.wordpress.org/Function_Reference/map_meta_cap

我花了几天时间试图用代码手动映射我的自定义上限。只要安装那个插件,映射你的CAP,一旦工作就停用。如果创建自定义角色,则需要Members 插件。

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

    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;
        }
    }
这将显示您实际上拥有哪些功能。

SO网友:T.Todua

对于自定义帖子类型,IDON\'T 建议使用挂钩:

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" ) 
   ...
}

结束

相关推荐

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(); 我不知道问题出在哪里?