Skip to content

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);
    }
  }
}

 

Dieser Beitrag hat einen Kommentar

  1. Hallo, ich wollte mit Ihrem Snippet unter WP 6.3.2 und PHP 8.1 die bbp_participant User role entfernen.
    Funktioniert leider nicht. Lasse ich mir einen Benutzer anzeigen, steht unten auf der Seite
    Zusätzliche Berechtigungen
    Berechtigungen: bbp_participant

    Ich habe das Snippet in die function.php meines Child Themes eingefügt.

Schreibe einen Kommentar

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

An den Anfang scrollen