WordPress at a glance

wp_dropdown_roles() WP 2.1.0

Print out <option> html elements for all user roles, for <select> element.

If you use this function on frontend, include the following files:

require_once ABSPATH . 'wp-admin/includes/user.php';
require_once ABSPATH . 'wp-admin/includes/template.php';
Works based on: get_editable_roles()

No Hooks.

Return

Null. Nothing.

Usage

wp_dropdown_roles( $selected );
$selected(string)
Slug for the role that should be already selected.
Default: ''

Examples

#1 Output roles select drop-down list for the user of the form

//require_once ABSPATH . 'wp-admin/includes/user.php';
//require_once ABSPATH . 'wp-admin/includes/template.php';

echo '<select name="foo">';
	wp_dropdown_roles();
echo '</select>';

Changelog

Since 2.1.0 Introduced.

Code of wp dropdown roles: wp-admin/includes/template.php WP 5.2.2

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

Related Functions

From tag: _dropdown (select options list)

More from category: Other

vladlu 100vlad.lu
Editors: kama 100
No comments
    Hello, !     Log In . Register