skip to Main Content

Clean unwanted Roles in WordPress

Clean Unwanted Roles In WordPress
geralt / Pixabay

Sometimes when a plugin is removed the additional user roles still remain. You might want to get rid of them. Unfortunately it is not that easy as one would expect. Here is how to remove unwanted user roles:

First of all, make sure you get the right names so you can adress them perfectly to remove them in the second step. Add this in your child themes function php and navigate to the backend to see the roles printed out:

//show roles
$wp_roles = new WP_Roles();
$names = $wp_roles->get_names();
print_r($names);
if ( class_exists( 'BackWPup' ) ) {
$wp_roles = new WP_Roles();
$wp_roles->remove_role("backwpup_admin");
$wp_roles->remove_role("backwpup_check");
$wp_roles->remove_role("backwpup_helper");
}

Now that you know the names delete them. Add them to the array before deleting them. This example contains the BBPress roles left after uninstalling the BBpress forum plugin.

add_action( 'admin_init', 'clean_unwanted_caps' );
function clean_unwanted_caps(){
  
//add your roles here in the array to delete them on preocession of this script
$delete_caps = array('bbp_keymaster ', 'bbp_participant');
  global $wp_roles;
  foreach ($delete_caps as $cap) {
    foreach (array_keys($wp_roles->roles) as $role) {
      $wp_roles->remove_cap($role, $cap);
    }
  }
}

 

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.

Back To Top