I have several users that I would like to put in user groups. The reason for this is each user group will have access to a specific set of pages or a specific custom post type. I would like to be able to limit each of the user groups to only have access (both by privilege and visibility) to the specific pages and post types that I choose.

I'd like to have a section of checkboxes in each of the pages/posts that has each user group/role listed. If the user group isn't checked, they won't be able to see or edit anything else in the admin.

These changes only apply to the admin. I want everyone to have access on the front end.

Here's the code I have so far. I'm creating two different roles and assigning to them only specific capabilities. The intent here is for each of the roles to only be able to see/edit pages to which they are given explicit access. In other words, I'd like the admin to be completely empty for them with the exception of the pages they are allowed to edit. I don't even want them to be able to see the other pages in the admin.

The code below adds the roles and assigns each of them the capabilities correctly, but they can both see all of the pages, and neither of them can edit any of the pages. I have custom meta associated with the pages they should be able to edit (i.e. 'allow_access'), and the meta returns an array of user ids that are allowed to edit the page. The meta works correctly and returns the array correctly, but the users are still not able to edit the page.

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;
}
  • Sorry, but plugin recommendations are off topic. Please see help center and How to Ask to see how you can document your efforts to solve this and where exactly you're stuck - so far it's just a task that you need solved. – kaiser Dec 6 '13 at 14:28
  • I understand. That's why I also asked if there was any way at all to do this. I'd prefer to do this in the theme files. I was just offering a plugin as a possible start to the solution. Either way, I need some help with the custom code to do this. – codescribblr Dec 7 '13 at 3:05
  • Then you should show us your research and what you've tried in code. Still: It's just a task and nothing more. – kaiser Dec 7 '13 at 13:42
  • Good point. I really didn't know where to start on this, so I spent a couple hours researching the ideas I was trying to accomplish and the code above is the result. Now I'm stuck. – codescribblr Dec 8 '13 at 1:38
  • Ha! Good by downvote, hello upvote :) – kaiser Dec 8 '13 at 3:04

PrivateContent - Multilevel Content Plugin

PrivateContent it’s a simple and fast solution to power up your wordpress website by adding a multilevel login, private areas, user private pages and users management features. It’s easy to use and doesn’t require any scripting knowledge!

  • A plain link to a commercial plugin without any statement about affiliation is considered spam. Flagged for removal. – kaiser Dec 6 '13 at 4:48
  • @kaiser What should I write if I am not connected to the author? – revoua Dec 6 '13 at 5:23
  • Explain how this plugin is going to help. Extract code from its source, sum it up here. In case that plugin isn't developed further, the answer will be useless and we have another link rotting. Keep in mind that this site is for developers, so please explain it for this group. Thanks. – kaiser Dec 6 '13 at 14:27

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.