1

After numerous days of trying to track down a problem in code, it turns out that the add_cap is only taking the first two in an array. The first one (editor) will only add delete_others_pages and delete_others_posts to the database. If I use the second method of repeating add_cap then I can get all of the capabilities into the database.

Can someone look at this code and see what is wrong? Why would only the first two items in the array go into the database?

        if( $new_roles == 'editor' ) {

            $user = new WP_User( $user_id );

            $user->add_cap(
                'delete_others_pages','delete_others_posts','delete_pages','delete_posts','delete_private_pages','delete_private_posts','delete_published_pages','delete_published_posts','edit_others_pages','edit_others_posts','edit_pages','edit_posts','edit_private_pages','edit_private_posts','edit_published_pages','edit_published_posts','manage_categories','manage_links','moderate_comments','publish_pages','publish_posts','read','read_private_pages','read_private_posts');

        }

        elseif( $new_roles == 'author' ) {

            $user_id = $XF->visitor->get('user_id');

            $user = new WP_User( $user_id );

            $user->add_cap(
                'delete_posts', 'delete_published_posts'
            );

            $user->add_cap(
                'edit_posts', 'edit_published_posts'
            );

            $user->add_cap(
                'publish_posts', 'read', 'upload_files'
            );

            $user->add_cap(
                'upload_files'
            );

        }

UPDATE CODE to place complete function: Adds arrays now so my initial question is answered but they are added to the wrong users.

public static function set_user_roles( &$classObj, $user_group_id, $secondary_group_ids ) {
    /**
     * Function set_user_roles(&$classObj, $user_group_id, $secondary_group_ids)
     * 
     * This function sets the current user role based on the secondary group
     */
    global $XF, $wp_roles;
    $user_groups = explode( ',', $secondary_group_ids );
    $user_groups[] = $user_group_id;

    /**
     * Loop through the user_roles and find the WP role equivalent for each
     */
    $user_roles = array();
    foreach( $user_groups as $user_group ) {
        $user_roles[] = $XF->options['xf_user_role'][$user_group];

        /**
         * wp_die( 'This is the role:' . var_dump( $XF->options['xf_user_role'] ) );
         * 
         * Returns:
         *
         * array(7) { [3]=> string(13) "administrator" [4]=> string(6) "author" [2]=> string(10) "subscriber" [1]=> string(10) "subscriber" [5]=> string(6) "author" [7]=> string(11) "contributor" [6]=> string(6) "editor" }
         */

        /**
         * wp_die( var_dump( $XF->options['xf_user_role'][$user_group] ) ); 
         *
         * Returns for user Ellie who has secondary group related to author
         * string(6) "author"
         * 
         * Returns Albert who has secondary group set to editor
         * string(6) "editor"
         *
         * Returns Pascal who has secondary group related to contributor
         * string(11) "contributor"
         *
         * Returns Gracie NO secondary group for registered members.
         * NULL
         *           
         */


        $new_roles =  $XF->options['xf_user_role'][$user_group];

        if( $new_roles == '' || $new_roles == null) {

            $user_id = $XF->visitor->get('user_id');

            $caps = array( 'read' );

            $user = new WP_User( $user_id );

            foreach ( $caps as $cap ) {

                $user->add_cap( $cap ); 

            }
            break;

        }

        elseif( $new_roles == 'subscriber' ) {

            $user_id = $XF->visitor->get('user_id');

            $caps = array( 'read' );

            $user = new WP_User( $user_id );

            foreach ( $caps as $cap ) {

                $user->add_cap( $cap ); 

            }
            break;

        }

        elseif( $new_roles == 'contributor' ) {

            $user_id = $XF->visitor->get('user_id');

            $caps= array( 'delete_posts', 'edit_posts', 'read' );

            $user = new WP_User( $user_id );

            foreach ( $caps as $cap ) {

                $user->add_cap( $cap ); 

            }
            break;

        }

        elseif( $new_roles == 'author' ) {          

            $user_id = $XF->visitor->get('user_id');

            $caps = array(
                'delete_posts', 'delete_published_posts','edit_posts', 'edit_published_posts','publish_posts', 'read', 'upload_files','upload_files'
                );

            $user = new WP_User( $user_id );

            foreach ( $caps as $cap ) {

                $user->add_cap( $cap ); 

            }
            break;

        }           

        elseif( $new_roles == 'editor' ) {

            $user_id = $XF->visitor->get('user_id');

            $caps = array ( 
                'delete_others_pages','delete_others_posts','delete_pages','delete_post','delete_private_pages','delete_private_posts','delete_published_pages','delete_published_posts','edit_others_pages','edit_others_posts','edit_pages','edit_posts', 'edit_private_pages','edit_private_posts','edit_published_pages','edit_published_posts','manage_categories','manage_links','moderate_comments','publish_pages','publish_posts','read','read_private_pages','read_private_posts'
            );

            $user = new WP_User( $user_id );

            foreach ( $caps as $cap ) {

                $user->add_cap( $cap ); 

            }
            break;

        }

        elseif( $new_roles == 'administrator' ) {

            $user_id = $XF->visitor->get('user_id');

            $caps = array ( 
                'activate_plugins', 'delete_others_pages','delete_others_posts','delete_pages','delete_plugins','delete_posts','delete_private_pages','delete_private_posts','delete_published_pages','delete_published_posts','edit_dashboard','edit_files','edit_others_pages','edit_others_posts','edit_pages','edit_posts','edit_private_pages','edit_private_posts','edit_published_pages','edit_published_posts','edit_theme_options','export','import','list_users','manage_categories','manage_links','manage_options','moderate_comments','promote_users','publish_pages','publish_posts','read_private_pages','read_private_posts','read','remove_users','switch_themes','upload_files','create_product'
            );

            $user = new WP_User( $user_id );

            foreach ( $caps as $cap ) {

                $user->add_cap( $cap ); 

            }
            break;

        }


    }

}

Update: Capabilities are being added but now all accounts are getting admin capabilities. This is now asked: Editor and contributor roles not correct after adding function

3

Looking at your code I think you are trying to modify the capabilities of existent roles. Do I'm right?

To do that you have to run the add_cap for a role object, not for a user object. Also, you must know that add_cap only accept one capability as string:

 $role = get_role( 'editor' );
 $role->add_cap( 'the_capability' );

If you want to add more than one capability you can perform a loop. For example:

$capabilities = array( 'cap_1', 'cap_2', 'cap_3' );
$role = get_role( 'editor' );

foreach( $capabilities as $cap ) {
        $role->add_cap( $cap );
}

The same apply to perform add_cap for a user object:

//example of user ID
$user_id = 25
$capabilities = array( 'cap_1', 'cap_2', 'cap_3' );
$user = new WP_User( $user_id );

foreach( $capabilities as $cap ) {
        $user->add_cap( $cap );
}
  • Thank you for the reply. This is for the current user and not the role. But even using a loop causes the same issue of only the first two in the array being added. I'm also seeing author capabilities being added to an editor instead of the editor capabilities. Something in my code is fouled but I don't see it,except you placed $capabilities before the new WP_User line. I'll try it. – LPH Feb 13 '14 at 12:11
  • Placing $user = new WP_User( $user_id ); after $capabilities seems to allow the complete array to be loaded. Thank you. This solves the one problem but unfortunately this code is adding editor capabilities to a subscriber and only subscriber to editor ... etc. – LPH Feb 13 '14 at 12:45
  • So, your question was answered, great!! But now, with your new question and updated code I don't understand what you are trying to do. Just remenber that adding capabilities don't remove previous capabilities, just add the new ones. Also, you can create new roles with the set of capabilities you want. It can be easier than modify default roles. – cybmeta Feb 15 '14 at 9:25
  • I have a current user logged into a forum. The secondary user group from the forum is mapped to a role. Now I want to add the capabilities to the user based on that mapped role. The problem is that the new code is assigning everyone admin capabilities. For example, I want secondary user group is editor therefore add editor capabilities. But instead its looping and giving read. Var_dump shows editor. Just did print_r and it is returning null editor null subscriber on the front WP page of test site. – LPH Feb 15 '14 at 12:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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