• 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'));
    
    $wp_roles->add_cap('administrator','miner_user');
    $wp_roles->add_cap('administrator','miner_supervisor');
    $wp_roles->add_cap('administrator','miner_administrator');
    
    $wp_roles->add_cap('editor','miner_user');
    $wp_roles->add_cap('editor','miner_supervisor');
    $wp_roles->add_cap('editor','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!

Viewing 2 replies - 16 through 17 (of 17 total)
  • Thread Starter Arnan de Gans

    (@shrikee)

    I see, hmm. Any suggestions as to how to get a specific capability to achieve that?

    The in_array() i’ve used wasn’t reliable at all i found out.

    Thread Starter Arnan de Gans

    (@shrikee)

    I’m giving up. In fact. I’m tempted to give up on wordpress for lacking documentation YET AGAIN.
    This whole thread (and forum for that matter) doesn’t bring anything useful at all.

    Got any idea on my last question? Which actually was my first question.

    How to construct a dropdown menu that works?

    *sighs in frustration*

Viewing 2 replies - 16 through 17 (of 17 total)
  • The topic ‘How to add new user roles and capabilities?’ is closed to new replies.