get_editable_roles(): array[]

Fetch a filtered list of user roles that the current user is allowed to edit.

Description

Simple function whose main purpose is to allow filtering of the list of roles in the $wp_roles object so that plugins can remove inappropriate ones depending on the situation or user making edits.
Specifically because without filtering anyone with the edit_users capability can edit others to be administrators, even if they are only editors or authors. This filter allows admins to delegate user management.

Return

array[] Array of arrays containing role information.

More Information

  • Which roles a user can assign are determined by passing all roles through the editable_roles filter.
  • The file that defines this function (wp-admin/includes/user.php) is only loaded in the admin sections.

Source

function get_editable_roles() {
	$all_roles = wp_roles()->roles;

	/**
	 * Filters the list of editable roles.
	 *
	 * @since 2.8.0
	 *
	 * @param array[] $all_roles Array of arrays containing role information.
	 */
	$editable_roles = apply_filters( 'editable_roles', $all_roles );

	return $editable_roles;
}

Hooks

apply_filters( ‘editable_roles’, array[] $all_roles )

Filters the list of editable roles.

Changelog

VersionDescription
2.8.0Introduced.

User Contributed Notes

  1. Skip to note 2 content

    Example

    Currently, you can assign the following roles:

    <dl>
      <?php foreach (get_editable_roles() as $role_name => $role_info): ?>
        <dt><?php echo $role_name ?></dt>
        <dd>
          <ul>
            <?php foreach ($role_info['capabilities'] as $capability => $_): ?>
              <li><?php echo $capability ?></li>
            <?php endforeach; ?>
          </ul>
        </dd>
      <?php endforeach; ?>
    </dl>

You must log in before being able to contribute a note or feedback.