Custom roles in WordPress not appearing in author lists
A quick little post from me today to explain a bug that stumped me for a couple of hours this morning in wordpress.
WordPress supports custom roles, either defined by a plugin or custom code in your plugin or theme, such as:
//Base the new role on the existing "editor" role $editor = get_role('editor'); //This function only ever does something once - the first time its run. //Once the 'staff member' role exists, it returns NULL each time. $staff_member = add_role('staffmember','Staff Member',$editor->capabilities);
The problem is, your nice shiny new user won’t appear in the author list on a post or page, so you can’t ever set them as the post/page author.
This is a known bug in wordpress that was reported 10 months ago (http://core.trac.wordpress.org/ticket/16841) and is the result of complications from changes wordpress have made to the user role system over the last few years.
The fix is actually relatively simple, but a touch backwards – You need to add a capability to the user of ‘level_1’, a deprecated role which shouldn’t be required, and actually does nothing but fix this bug.
//Get the role again - need to do this incase the role already exists, and add_role returned null. $staff_member = get_role('staffmember'); $staff_member->add_cap('level_1');
Any new users with the role of “Staff Member” will now appear in the list. Another issue applies to existing users, though. While they’ll have the new level_1 capability, wordpress won’t update them in its database to have the old style user_level option (which is now deprecated, anyway).
The fix for existing users is a bit lame: You need to change them to be another type of role, and then back to Staff Members – I think this is a bug in itself, and have submitted the issue to the wordpress developers (http://core.trac.wordpress.org/ticket/19747).
I hope this helps some of you out!