自定义发布类型功能--启用编辑、禁用创建和删除

时间:2015-04-22 作者:jdp

我正在尝试为自定义帖子类型配置功能。我想启用编辑,但禁用创建和删除。create_posts 不能设置为false,除非delete_posts 也为false,但这种组合似乎禁用了编辑屏幕。

谢谢

2 个回复
SO网友:bueltge

有很多功能,你可以为你的帖子类型设置。

foreach ( self::$todo_roles as $role ) {            
     $wp_roles->add_cap( $role, \'edit_\' . self::$post_type_1 );
     $wp_roles->add_cap( $role, \'read_\' . self::$post_type_1 ); 
     $wp_roles->add_cap( $role, \'delete_\' . self::$post_type_1 );
     $wp_roles->add_cap( $role, \'edit_\' . self::$post_type_1 . \'s\' );
     $wp_roles->add_cap( $role, \'edit_others_\' . self::$post_type_1 . \'s\' );    
     $wp_roles->add_cap( $role, \'publish_\' . self::$post_type_1 . \'s\' );
     $wp_roles->add_cap( $role, \'read_private_\' . self::$post_type_1 . \'s\' );   
     $wp_roles->add_cap( $role, \'delete_\' . self::$post_type_1 . \'s\' ); 
     $wp_roles->add_cap( $role, \'delete_private_\' . self::$post_type_1 . \'s\' ); 
     $wp_roles->add_cap( $role, \'delete_published_\' . self::$post_type_1 . \'s\' );
     $wp_roles->add_cap( $role, \'delete_others_\' . self::$post_type_1 . \'s\' );  
     $wp_roles->add_cap( $role, \'edit_private_\' . self::$post_type_1 . \'s\' );
     $wp_roles->add_cap( $role, \'edit_published_\' . self::$post_type_1 . \'s\' ); 
} 
使用类似于上述的功能,您可以为角色添加功能。

作为对源的提示。这个self::$post_type_1 是自定义帖子类型类中的变量。这个foreach 循环是将此功能经常添加到不同的角色。

SO网友:MarcGuay

以下是我为实现这一目标所做的工作。我将使用“blob”作为示例自定义帖子类型。呼叫时register_post_type, 包括以下参数:

"capability_type" => "blob",
\'capabilities\' => array(
    \'create_posts\' => \'create_blob\',
    \'publish_posts\' => \'publish_blob\',
    \'edit_posts\' => \'edit_blob\',
    \'edit_others_posts\' => \'edit_others_blob\',
    \'delete_posts\' => \'delete_blob\',
    \'delete_others_posts\' => \'delete_others_blob\',
    \'read_private_posts\' => \'read_private_blob\',
    \'edit_post\' => \'edit_blob\',
    \'delete_post\' => \'delete_blob\',
    \'read_post\' => \'read_blob\',
),
"map_meta_cap" => false,
您还需要以下代码来映射元功能。Source.

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 blob, get the post and post type object. */
    if ( \'edit_blob\' == $cap || \'delete_blob\' == $cap || \'read_blob\' == $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 blob, assign the required capability. */
    if ( \'edit_blob\' == $cap ) {
        if ( $user_id == $post->post_author )
            $caps[] = $post_type->cap->edit_posts;
        else
            $caps[] = $post_type->cap->edit_others_posts;
    }

    /* If deleting a blob, assign the required capability. */
    elseif ( \'delete_blob\' == $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 blob, assign the required capability. */
    elseif ( \'read_blob\' == $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;
}
现在你需要一个插件来编辑你定义的新的自定义功能。我用过Members. 使用该插件向用户角色授予或拒绝新功能。对于Members插件和您允许编辑但阻止创建和删除的特定请求,这涉及打开用户->角色菜单项,编辑角色,单击左侧菜单中的“Blob”以筛选与Blob相关的功能,然后拒绝“create\\u Blob”、“delete\\u blogs”和“delete\\u others\\u Blob”。一旦它们被设置好,并且您对它们感到满意,您就可以禁用插件,这些功能将存储在数据库中。

结束

相关推荐

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