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);
Comments: 6
Thanks David, this is just what I was trying to do and your code worked for me.
Thanks very much.
Works perfectly! Thank you very much for this.
Thanks David! I’ve been trying to find a solution to this problem for a while.
Thank you David, it works for me too 🙂
Handling the Gutenberg side of things was exactly what I was looking for.
Thank you!
I thought I was going crazy and headed down a caching angle as wp_dropdown_users_args made no difference. Then I stumbled across this, thank you namesake.