0

I created a role, 'CFI_G' in a plugin activator:

if(!role_exists('cfi_g')){ add_role('cfi_g' , 'CFI-G',
array('edit_gc_instruction')); } else {

(note the underscore in the slug)

It appeared to all go correctly and I have assigned several users to that role. However I need to loop through all roles in the site to create a select list, so I used $wp_roles:

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

The CFI-G role does not appear in $editable_roles. However I tried adding the role of "CFI-G" using the MEMBERS plugin which created it with the slug "cfi-g". Now both the new cfi-g and the cfi_g roles appear in $editable_roles. If I delete the 'cfi-g' role, 'cfi_g' again disappears from $editable_roles.

Is an underscore NOT to be used in slugs? is there a way I can keep 'cfi_g'? Might this possibly be a bug?

thanks

  • 1
    I'd suggest you look into the members plugin code to see if it Is doing anything that you are not (an extra filter on editable_roles perhaps?) it is unlikely it is the underscore as codex has examples using them. have you tried adding the role with the hyphen using your own code instead of the members plugin? process of elimination and all that. nice username btw ;-) – majick Mar 19 '19 at 0:41
  • 1
    also when your code runs is important for add_role. see developer.wordpress.org/reference/functions/add_role/… – majick Mar 19 '19 at 0:51
  • Code to add the role runs at plugin activation. I tried editing cfi_g directly in the data base. Vanished completely, Members plugin has a lot going on, taking me some time to work through it. Your username not bad either. – dsj Mar 19 '19 at 1:43
  • It really wouldn't hurt just to call the add_role code in the init hook rather than on plugin activation... it will not add the role if it already exists, but will if it doesn't. better than it disappearing all the time. – majick Mar 19 '19 at 5:12
  • I'm confused. I just realized I am using the exact same code in two places. In a form to generate a list of roles, and In the form handler to validate a list of roles returned from the form. In the form; $editable_roles is correct and includes 'CFI-G' however in the handler with a copy of the same code it does not. I must have an error somewhere...... – dsj Mar 19 '19 at 15:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.