Need help with plugin, need dropdown list of selectable roles (1 post)

  1. Arnan de Gans
    Posted 5 years ago #

    For a plugin i'm making i need some help with selecting user roles.

    I create/add some capabilities i add to existing roles which works fine. I also create a new userrole. Which also works fine.
    The problem lies in selecting that role.
    I've made a dropdown menu which lists all roles but selecting the right role often results in wrong access rights because the dropdown doesn't work right.

    My current code:

    $all_roles 		= $wp_roles->roles;
    $editable_roles = apply_filters('editable_roles', $all_roles);

    And the dropdown:

    <select name="miner_corp_statistics">
    	<?php foreach($editable_roles as $role) { ?>
    		<?php $capabilities = array_keys($role['capabilities']); ?>
    		<option value="<?php echo current($capabilities); ?>" <?php if(in_array($miner_config['corp_statistics'], $capabilities)) { echo 'selected'; } ?>><?php echo $role['name']; ?></option>
    	<?php }	unset($role);?>
    </select> Minimum required userlevel to see the statistics.

    This in essence works fine. But since it relies on in_array() its hardly reliable. For example if i select the subscriber role i almost always end up assigning my newly made role. Which obviously is wrong. I suspect this happens because of the capabilities assigned.

    The roles and capabilities are created using this code on plugin activation:

    add_role('miner_user', 'Corp Miner', array('read', 'miner_user'));
    	add_role('miner_supervisor', 'Corp Supervisor', array('read', 'miner_user', 'miner_supervisor'));
    	add_role('miner_administrator', 'Corp Administrator', array('read', 'miner_user','miner_supervisor', 'miner_administrator'));

    Perhaps i'm missing something, perhaps i did it wrong entirely. I'm not sure. I've spend all evening on the codex and google yesterday with zero results on the dropdown. I did found out my way of adding the roles and capabilities is right.

    Any help is appreciated. Some tutorial, snippet, manual... Something to make this work. Thanks!

Topic Closed

This topic has been closed to new replies.

About this Topic