Looking at your code I think you are trying to modify the capabilities of existent roles. Am I 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 );
}
Related Posts:
- What action should I hook into when adding roles and capabilities?
- What is valid timing of using current_user_can() and related functions?
- Allow Contributor to change the author of his own post?
- What’s the difference between Role and Meta capabilities; When to use map_meta_cap() filter
- How can I grant capabilities directly to users (not roles) in wp-admin?
- Disable posts, only allow to edit existing pages, not create new ones (create_posts)
- How to get all capabilities of an existing user role
- What exactly is WordPress?
- Menu capability in WordPress
- Does the “promote_users” capability allow someone to create a new admin account?
- current_user_can( ‘edit_post’, $post_id ) does not work for contributer but for administrator
- Add user capability and check against it
- Listing all capabilities in dropdown is returning boolean
- Add Media Upload Capabilities Needed for Custom Role for non-Posts
- How can I prevent a writer from being able to edit an article that has been scheduled?
- Check whether user can delete a given post
- Multiple Authors on Single Post
- Allow unfiltered HTML in titles for low level users?
- Allow users to set a post author
- Unify the roles and caps?
- current_user_can capabilities in the admin not working as expected
- Remove menu items from admin page and limit capabilities
- Filter list of rules based on a capability
- Using author_can() on custom post types in WordPress
- How to show a admin bar menu item only to users with certain capabilities?
- Restricted user capabilities cannot add image
- User capability per post
- How come I see a manage_categories capability but not a manage_tags capability for any users?
- Theme option editing capability problems
- Allow Contributor to edit published post and filter by page id
- Usage of current_user_can()
- Is it wise not to use only meta capabilities for certain basic post types?
- Remove capability from specific user
- What capabilities are assigned to unauthenticated users?
- Capabilities Not Changing
- Assign multiple roles , overlapping capabilities
- Administrator role capabilities
- How to get all capabilities
- Prevent all users from editing posts except admins using hooks
- WordPress remove capability post ,media completely for custom role
- Add capability to “Editor” role to be able to view all users attachments
- Why cannot author edit pages?
- Add capability to a role , so user is only able to view his own posts
- Plugin page and capabilities
- how to change capability required to view an admin page? [duplicate]
- How can restrict certain users from delete a category but can view only in wordpress?
- Delete admin edit/delete links in users list wordpress admin
- Create a Capability category to group a few custom Capabilities
- Capability to edit post slugs
- admin can not change author profile picture
- Changing capability type without altering plugin
- Can’t create new Pods pages
- Is there a way of retrieving the core WP capabilities?
- Pricing Tables admin page capability
- Best Roles and Capability on a site with review features using a front end custom post
- Not able to give editors acess to new admin menu item
- Manage Roles and Capabilities on Multi-site
- Force logout when role is changed
- add_role() run only once?
- Why is javascript allowed in my post content?
- Adding capabilities to super admins
- Email notification for editors only
- How do you add custom taxonomy capabilities exactly?
- How to prevent users with “edit_others_posts” capability from editing admin posts
- How can I have different groups of editors only allowed to edit certain parent+subpages?
- User capability of create page/new page
- Including only current user’s posts in search
- Remove wordpress author’s capability to moderate comments on their own posts
- current_user_can() always returns true if user is super admin
- Getting users by specific capability, not role
- Cannot attach media when capabilities added to custom post type
- Add condition of user capability in WP_query
- Role capability delete multiple post type posts doesn’t work
- WordPress bug with capabilities?
- Define new user capability for custom post types?
- User Role Capabilities for WordPress Gutenberg Blocks
- Allow add new post access to custom post but not wp post for some role
- Stop users of author role from editing already pending posts
- How to add custom JavaScript in functions?
- Role capabilities issue
- Want to know parameters that can be passed to user_can() for access control by user capabilities
- Assigning multiple or additional capabilities to specific users or how to create additional roles like bbpress roles?
- How To Create A File Archive in WordPress?
- custom post type capabilities – needs to be VERY secure
- User role editor – Add download files capability
- How to fix the Post Preview Button (CPT & map_meta_cap)
- Restrict Custom Post Type per role in Dashboard
- Can’t see post status drop down for custom post type
- custom taxonomy not showing up after adding capabilities upon updating to WP 5.1.1
- Not able to post data through wp-admin
- How can I create multiple different admin roles with their own capabilities
- Groups roles & capabilities
- How can I remove “Add new” button on custom post type
- Why does user_can return false for a capability during plugin deactivation?
- Capabilities and mapping required for a role to be able to edit other’s posts of a custom type, BUT only be able to edit their own blog posts
- Disable `create_post` for built-in post type
- Weird capabilities / roles behavior
- Capability to edit own post, but not create new one
- Non-super-admin users cannot access CPT even though I have explicitly added the capabilities to the user role
- Restrict APP REST API for users with account and capabilities