Retrieve user roles but exclude default roles

I am currently using the wp_roles() function to retrieve all user roles available like this:

?php
  foreach (wp_roles()-get_names() as $role) {
    echo translate_user_role( $role );
  }
?

How would I retrieve an array/list of all custom roles that I've created without the default roles (Administrator, Editor, Subscriber, etc) included?

Topic user-roles Wordpress

Category Web


So I ended up using a combination of the array_filter function and the in_array function to retrieve all custom roles like this:

<?php

  function check_roles($userRole) {
    $default_roles = array('Administrator', 'Editor', 'Author', 'Contributor', 'Subscriber');
    if (!in_array($userRole, $default_roles)) {
      return $userRole;
    }
  }

  $all_roles = wp_roles()->get_names();
  $custom_roles = array_filter($all_roles, 'check_roles');

  foreach ($custom_roles as $role) {
    echo $role;
  }

?>

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.