wp_dropdown_roles( string $selected = '' )

Print out option html elements for role selectors.


Description Description


Parameters Parameters

$selected

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

Default value: ''


Top ↑

Source 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

Changelog
Version Description
2.1.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by Codex

    Example

    If you would like to set a default role for the dropdown, provide the slug for the desired role as a parameter. The following example creates a dropdown of user roles with the “Editor” role as the default, selected value:

    <select>
       <?php wp_dropdown_roles( 'editor' ); ?>
    </select>
    

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