是否将用户限制为只能访问指定的页面?

时间:2013-12-06 作者:codescribblr

我有几个用户,我想把他们放在用户组中。原因是每个用户组都可以访问特定的页面集或特定的自定义帖子类型。我希望能够限制每个用户组只能访问(通过权限和可见性)我选择的特定页面和帖子类型。

我希望在列出每个用户组/角色的每个页面/帖子中都有一部分复选框。如果未选中用户组,他们将无法在管理中查看或编辑任何其他内容。

这些更改仅适用于管理员。我希望每个人都能访问前端。

这是我目前掌握的代码。我正在创建两个不同的角色,并只为它们分配特定的功能。这里的目的是让每个角色只能查看/编辑他们被授予显式访问权限的页面。换句话说,除了允许他们编辑的页面之外,我希望管理员对他们来说是完全空的。我甚至不想让他们看到管理中的其他页面。

下面的代码添加了角色并正确地为每个角色分配了功能,但它们都可以看到所有页面,并且都不能编辑任何页面。我有与他们应该能够编辑的页面关联的自定义元(即“allow\\u access”),该元返回一个允许编辑页面的用户ID数组。meta工作正常并正确返回数组,但用户仍然无法编辑页面。

add_action(\'init\', \'taylors_add_roles\');
function taylors_add_roles() {
    if(!$GLOBALS[\'wp_roles\']->life_stages_children) {
        add_role(
            \'life_stages_children\',
            __( \'Life Stages - Children\' ),
            array(
                \'read\'         => true,
                \'edit_posts\'   => false,
                \'delete_posts\' => false,
            )
        );
    }
    if(!$GLOBALS[\'wp_roles\']->life_stages_students) {
        add_role(
            \'life_stages_students\',
            __( \'Life Stages - Students\' ),
            array(
                \'read\'         => true,
                \'edit_posts\'   => false,
                \'delete_posts\' => false,
            )
        );
    }
}

add_action(\'init\', \'taylors_add_cap\');
function taylors_add_cap() {
    $custom_cap[\'life_stages_children\'][] = \'edit_life_stages_children_pages\';
    $custom_cap[\'life_stages_children\'][] = \'delete_life_stages_children_pages\';
    $custom_cap[\'life_stages_children\'][] = \'delete_others_life_stages_children_pages\';
    $custom_cap[\'life_stages_children\'][] = \'read_life_stages_children_pages\';
    $custom_cap[\'life_stages_children\'][] = \'read_private_life_stages_children_pages\';
    $custom_cap[\'life_stages_children\'][] = \'edit_others_life_stages_children_pages\';
    $custom_cap[\'life_stages_students\'][] = \'edit_life_stages_students_pages\';
    $custom_cap[\'life_stages_students\'][] = \'delete_life_stages_students_pages\';
    $custom_cap[\'life_stages_students\'][] = \'delete_others_life_stages_students_pages\';
    $custom_cap[\'life_stages_students\'][] = \'read_life_stages_students_pages\';
    $custom_cap[\'life_stages_students\'][] = \'read_private_life_stages_students_pages\';
    $custom_cap[\'life_stages_students\'][] = \'edit_others_life_stages_students_pages\';

    foreach ($custom_cap as $role => $caps){
        foreach ($caps as $cap){
            if(!$GLOBALS[\'wp_roles\']->role_objects[$role]->has_cap($cap)) {
                $GLOBALS[\'wp_roles\']->role_objects[$role]->add_cap($cap);
            }
        }
    }

}

add_filter( \'map_meta_cap\', \'taylors_map_meta_cap\', 10, 4 );
function taylors_map_meta_cap( $caps, $cap, $user_id, $args ) {

    /* If editing, deleting, or reading a page, get the post and post type object. */
    if ( \'edit_pages\' == $cap || \'delete_pages\' == $cap || \'read_pages\' == $cap ) {
        $post = get_post( $args[0] );
        $post_access = get_post_meta($post->ID, \'allow_access\', true); //an array containing the users allowed to access this post.
        $post_type = get_post_type_object( $post->post_type );

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

    /* If editing a page, assign the required capability. */
    if ( \'edit_pages\' == $cap && $post->post_type == \'page\' && in_array(get_current_user_id(), $post_access) ) {
        if ( $user_id == $post->post_author )
            $caps[] = $post_type->cap->edit_life_stages_children_pages;
        else
            $caps[] = $post_type->cap->edit_others_life_stages_children_pages;
    }

    /* If deleting a page, assign the required capability. */
    elseif ( \'delete_pages\' == $cap && $post->post_type == \'page\' && in_array(get_current_user_id(), $post_access) ) {
        if ( $user_id == $post->post_author )
            $caps[] = $post_type->cap->delete_life_stages_children_pages;
        else
            $caps[] = $post_type->cap->delete_others_life_stages_children_pages;
    }

    /* If reading a private page, assign the required capability. */
    elseif ( \'read\' == $cap && $post->post_type == \'page\' && in_array(get_current_user_id(), $post_access) ) {

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

    /* Return the capabilities required by the user. */
    return $caps;
}

1 个回复
SO网友:revoua

PrivateContent - Multilevel Content Plugin

PrivateContent通过添加多级登录、私有区域、用户私有页面和用户管理功能,为wordpress网站提供简单快速的解决方案。它易于使用,不需要任何脚本知识!

结束

相关推荐

Recommended File Permissions

嘿,伙计们,我花了很长时间试图解决这个问题。我想知道WordPress中的文件权限应该是什么样子in order to use the autoupdate feature. 到目前为止,我的wordpress安装程序一直在询问我的FTP信息,我不想使用那种升级/安装方法,我想使用纯/直接PHP。某些上下文:Web服务器和php fcgi守护程序运行为www-data:www-data</wordpress安装位于/home/blaenk/sites/domain.tld/</首先,我read