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 usesget_editable_roles()
to fetch the needed roles. It does nothing else than fetching the roles from the global:and filter it afterwards:
So you can simply add the following function to a plugin or your
functions.php
file of your theme:Then, right before the call to
wp_dropdown_roles()
, you add: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.