Do not use numeric role names

I do not recommend you to use numeric role names, e.g. ‘1000’, ‘150’, etc. You may create such role, may fill it with capabilities with the help of current and previous versions of “User Role Editor” and may be other plugins. But WordPress will not allow you to use such roles. You will get error message, like this: “You can’t give users that role”.

Why it is happened.

WordPress uses function wp_dropdown_roles() to generate dropdown list of available role.
You may see such dropdown list above of the users list at the “Users” page:

Roles dropdown list at the top of the Users page
Roles dropdown list at the top of the Users page

or at the user profile:
Roles dropdown list at user profile page
Roles dropdown list at user profile page

Look into code of wp_dropdown_roles() function (file /wp-admin/includes/template.php):

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

	$editable_roles = array_reverse( get_editable_roles() );

Line #759 is interesting for us. If PHP function array_reverse() is called without 2nd argument equal “true”, it resets numeric array indexes. That is if array element with index “150” will be placed at the 1st place in the resulting array, it get index “0” instead of initial “150”. Now imagine that was not just numeric array index, but your lovely new created digital role name “150”. Of course WordPress could not find role with ID=’0′ later and can not assign it to the user. That’s why you get error message “You can’t give users that role” in case with numeric role name.

Deprecated user level

Other reason why you shoud not use numeric user role name is user levels. Yes user levels are deprecated but still exist in WordPress code for the backward compatibility with old versions.
If function curren_user_can() will get numeric parameter it is interpreted as user level value.

function has_cap( $cap ) {
	if ( is_numeric( $cap ) ) {
		_deprecated_argument( __FUNCTION__, '2.0', __('Usage of user levels by plugins and themes is deprecated. Use roles and capabilities instead.') );
		$cap = $this->translate_level_to_cap( $cap );
        }
function translate_level_to_cap( $level ) {
    return 'level_' . $level;
}

I think that you guessed already, can WordPress find role with name ‘level_150’ if it has just 10 user levels by default. No, it can not.
That’s it. Do not use numeric role names.
I will help you with it blocking creating of new roles with numeric names at the next version of User Role Editor WordPress plugin.

Share