Understanding user capabilities
Capabilities can be considered as tasks that users are permitted to perform inside the application. A single user role can perform many capabilities, while a single capability can be performed by many user roles. Typically, we use the term access control for handling capabilities in web applications. Let's see how capabilities work inside WordPress.
Creating your first capability
Capabilities are always associated with user roles, and hence we cannot create new capabilities without providing a user role. Let's look at the following code for associating a custom capability with a follower user role created in the Creating user roles for a application section:
public function add_application_user_capabilities(){ $role = get_role( 'follower' ); $role->add_cap( 'follow_developer_activities' ); }
First, we need to retrieve the user role as an object using the get_role
function. Then, we can associate a new or existing capability...