wp_dropdown_roles() WordPress Function

The wp_dropdown_roles() function displays a drop-down list of user roles on the WordPress platform. This function is useful for allowing users to select their role when creating new user accounts or editing their profile.

wp_dropdown_roles( string $selected = '' ) #

Prints out option HTML elements for role selectors.


Parameters

$selected

(string)(Optional)Slug for the role that should be already selected.

Default value: ''


Top ↑

Source

File: wp-admin/includes/template.php

function wp_dropdown_roles( $selected = '' ) {
	$r = '';

	$editable_roles = array_reverse( get_editable_roles() );

	foreach ( $editable_roles as $role => $details ) {
		$name = translate_user_role( $details['name'] );
		// Preselect specified role.
		if ( $selected === $role ) {
			$r .= "\n\t<option selected='selected' value='" . esc_attr( $role ) . "'>$name</option>";
		} else {
			$r .= "\n\t<option value='" . esc_attr( $role ) . "'>$name</option>";
		}
	}

	echo $r;
}


Top ↑

Changelog

Changelog
VersionDescription
2.1.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.