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

Removing existing user roles

We should have the ability to remove existing or custom user roles when necessary. WordPress offers the remove_role function for deleting both custom and existing user roles. In this case, we want to get rid of existing user roles. Also, there can be situations where you use a plugin with specific user roles and suddenly you want to disable the functionality of the plugin. In both cases, we need to remove the user roles from the database. Let's create a function that removes the unnecessary user roles from the system. Add the following function to the WPWAF_Config_Managerclass, as illustrated in the following code:

    public function remove_application_user_roles(){ 
remove_role( 'author' );
remove_role( 'editor' );
remove_role( 'contributor' );
remove_role( 'subscriber' );
}

As mentioned earlier, the remove_role function involves database operations and hence it's wise to use it with the register_activation_hook function. So we can call this function from the activation_handler function of the WPWAF_Config_Manager class as it's responsible for handling all activation-related features.

This code will remove the existing roles and you can't get them back without importing them again to the database. So I recommend you only add the code on a test site or just comment the code.

In this section, we looked at how user roles work in WordPress. Now, we need to see how we can associate capabilities with these user roles.