plugin development - wp_dropdown_roles() to replace option value = code

admin2025-01-08  6

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 ()?

Share Improve this question asked Mar 14, 2014 at 5:00 LPHLPH 8081 gold badge11 silver badges25 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736270536a1435.html

最新回复(0)