I'm trying to replace a hardcoded dropdown list of roles in a plugin. Currently this is the code:
echo '<select name="xf_user_role_' . $id . '" id="xf_user_role_' . $id . '">';
?>
<option value='subscriber'
<?php echo ($xf_options['xf_user_role'][$id] == 'subscriber') ? "selected='yes'" : ''; ?> >Subscriber</option>
<option value='contributor'
<?php echo ($xf_options['xf_user_role'][$id] == 'contributor') ? "selected='yes'" : ''; ?> >Contributor</option>
<option value='author'
<?php echo ($xf_options['xf_user_role'][$id] == 'author') ? "selected='yes'" : ''; ?> >Author</option>
<option value='editor'
<?php echo ($xf_options['xf_user_role'][$id] == 'editor') ? "selected='yes'" : ''; ?> >Editor</option>
<option value='administrator'
<?php echo ($xf_options['xf_user_role'][$id] == 'administrator') ? "selected='yes'" : ''; ?> >Administrator</option>
</select>
This code is changed to :
echo '<select name="xf_user_role_' . $id . '" id="xf_user_role_' . $id . '">';
wp_dropdown_roles( );
?>
</select>
The dropdown list shows but selecting doesn't give the $xf_options ...
The wp_dropdown_roles isn't well documented in the Codex page. I've tried a few different ways to add information into the () but am not getting it right.
What is the proper information for within the ()?
I'm trying to replace a hardcoded dropdown list of roles in a plugin. Currently this is the code:
echo '<select name="xf_user_role_' . $id . '" id="xf_user_role_' . $id . '">';
?>
<option value='subscriber'
<?php echo ($xf_options['xf_user_role'][$id] == 'subscriber') ? "selected='yes'" : ''; ?> >Subscriber</option>
<option value='contributor'
<?php echo ($xf_options['xf_user_role'][$id] == 'contributor') ? "selected='yes'" : ''; ?> >Contributor</option>
<option value='author'
<?php echo ($xf_options['xf_user_role'][$id] == 'author') ? "selected='yes'" : ''; ?> >Author</option>
<option value='editor'
<?php echo ($xf_options['xf_user_role'][$id] == 'editor') ? "selected='yes'" : ''; ?> >Editor</option>
<option value='administrator'
<?php echo ($xf_options['xf_user_role'][$id] == 'administrator') ? "selected='yes'" : ''; ?> >Administrator</option>
</select>
This code is changed to :
echo '<select name="xf_user_role_' . $id . '" id="xf_user_role_' . $id . '">';
wp_dropdown_roles( );
?>
</select>
The dropdown list shows but selecting doesn't give the $xf_options ...
The wp_dropdown_roles isn't well documented in the Codex page. I've tried a few different ways to add information into the () but am not getting it right.
What is the proper information for within the ()?
The wp_dropdown_roles()
function internally uses get_editable_roles()
to fetch the needed roles. It does nothing else than fetching the roles from the global:
$all_roles = $GLOBALS['wp_roles']->roles;
and filter it afterwards:
return apply_filters( 'editable_roles', $all_roles );
So you can simply add the following function to a plugin or your functions.php
file of your theme:
function wpse137935_filter_editable_roles( Array $roles )
{
// Don't conflict and remove immediately
remove_filter( current_filter(), __FUNCTION__ );
// Do any logic in here that appends or removes roles
return $roles;
}
Then, right before the call to wp_dropdown_roles()
, you add:
add_filter( 'editable_roles', 'wpse137935_filter_editable_roles' );
It's important that you add the filter as late as possible to not crash into other plugins or your theme if they/it calls the same function elsewhere.