Using WordPress ‘remove_role()’ PHP function

The remove_role() WordPress PHP function removes a specified role from the WordPress installation, if it exists.

Usage

To use the function, simply call it with the role you want to remove:

remove_role('role_name');

Parameters

  • $role (string): The name of the role you want to remove.

More information

See WordPress Developer Resources: remove_role()

Examples

Remove the ‘subscriber’ role

This example removes the ‘subscriber’ role from your WordPress installation:

remove_role('subscriber');

Remove a custom role

This example removes a custom role named ‘content_editor’:

remove_role('content_editor');

Remove a role conditionally

This example checks if a role named ‘limited_admin’ exists, and removes it if so:

if (get_role('limited_admin')) {
    remove_role('limited_admin');
}

Remove multiple roles

This example removes multiple roles at once:

$roles_to_remove = array('role1', 'role2', 'role3');

foreach ($roles_to_remove as $role) {
    remove_role($role);
}

Remove a role and add a new one

This example removes the ‘editor’ role and adds a new custom role with the same capabilities:

if (get_role('editor')) {
    $editor_capabilities = get_role('editor')->capabilities;
    remove_role('editor');
    add_role('custom_editor', 'Custom Editor', $editor_capabilities);
}