WordPress Authors Dropdown & Custom Roles

I’ve been using the Members plugin on a client’s WordPress site to define custom roles. It’s been working great except for one thing; the authors dropdown on the Gutenberg document editor does not show anyone who has a custom role.

I had a look around and found several posts that suggested using the the wp_dropdown_users_args filter which worked for the Quick Edit author dropdown but not for the main document editor. In fact I couldn’t find anybody with a solution for that since Gutenberg was introduced. I managed to suss it out for myself and thought I’d share the code for anyone stuck in a similar situation. The following goes inside functions.php. Make sure to replace “custom_role_one” and “custom_role_two” with your actual role names.

// Handles the Quick Edit authors dropdown
function display_custom_roles_in_author_dropdown($query_args, $r)
{
	if (isset($r['name']) && ($r['name'] === 'post_author_override' || $r['name'] === 'post_author')) {
		if (isset($query_args['who'])) {
			unset($query_args['who']);
        }
        $query_args['role__in'] = ['administrator', 'author', 'editor', 'custom_role_one', 'custom_role_two'];
    }
    return $query_args;
}
add_filter('wp_dropdown_users_args', 'display_custom_roles_in_author_dropdown', 10, 2);

// Handles the Gutenberg document editor authors dropdown
function display_custom_roles_in_gutenberg_author_dropdown($prepared_args, $request = null)
{
	if (isset($prepared_args['who']) && $prepared_args['who'] === 'authors') {
		unset($prepared_args['who']);
		$prepared_args['role__in'] = ['administrator', 'author', 'editor', 'custom_role_one', 'custom_role_two'];
	}
    return $prepared_args;
}
add_filter('rest_user_query', 'display_custom_roles_in_gutenberg_author_dropdown', 10, 2);
Share Article

Comments: 6

Leave a reply

Please fill in the form fields and improve Skynet by completing the captcha!

Thank you! All comments are moderated so yours will appear shortly.

When i joined the core we didn’t have any fancy shmancy tanks… we had sticks! Two sticks and a rock for the whole platoon, and we had to share the rock! Buck up boy, your one very lucky marine.

— Sgt. Johnson, Halo 2