Wordpress Web Application Development(Third Edition)
上QQ阅读APP看书,第一时间看更新

Creating application user roles

As planned earlier, we will need three types of user roles for our application to handle: moderators, premium members, and free members. So, we can update our plugin by adding a specific function to create the user roles, as shown in the following code:

    public function add_application_user_roles() { 
add_role( 'wpwaf_premium_member', __('Premium Member','wpwaf'), array( 'read' => true ) );
add_role( 'wpwaf_free_member', __('Free Member','wpwaf'), array( 'read' => true ) );
add_role( 'wpwaf_moderator', __('Moderator','wpwaf'), array( 'read' => true ) );
}

Application user roles are created with the default capability of read, used by all the user roles in WordPress. Integration of this function to our main plugin will be explained in the next few sections.

You can use normal words such as member and moderator for the custom user roles created in the plugins. However, these role keys may also be used in other popular plugins for different purposes. If we use two plugins with same user role intended for different features, there will be a conflict. Therefore, it's better to add a prefix and make your user roles unique, to avoid conflicts and be compatible with popular plugins.