Understanding user capabilities
Capabilities can be considered as tasks, which 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 custom capability with a follower user role, created in the earlier section, Creating application user roles:
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 new or existing capability using the add_cap
function...