get_editable_roles() WordPress Function

The get_editable_roles() function is a built-in WordPress function. It returns an array of roles that the current user is allowed to edit.

get_editable_roles() #

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.


Top ↑

Return

(array[]) Array of arrays containing role information.


Top ↑

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.

Top ↑

Source

File: wp-admin/includes/user.php

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


Top ↑

Changelog

Changelog
VersionDescription
2.8.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.