Handles editor authors dropdown

By

Handles the Gutenberg document editor authors dropdown

function display_custom_roles_in_gutenberg_author_dropdown($query_args, $request = null)
{
	if (isset($query_args['who']) && $query_args['who'] === 'authors') {
		unset($query_args['who']);
		$query_args['role__in'] = ['administrator', 'author', 'editor', 'custom_role_one', 'custom_role_two'];
	}
    return $query_args;
}
add_filter('rest_user_query', 'display_custom_roles_in_gutenberg_author_dropdown', 10, 2);

Handles the Quick Edit authors dropdown

function display_custom_roles_in_author_dropdown($query_args, $request)
{
	if (isset($request['name']) && ($request['name'] === 'post_author_override' || $request['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);

Override authors dropdown no matter in Quick Edit or Gutenbreg

function switchAuthor($output)
{

    //global $post is available here, hence you can check for the post type here
    $users = get_users(array( 'role__in' => array( 'subscriber' ) ) );

    $output = "<select id=\"post_author_override\" name=\"post_author_override\" class=\"\">";

    //Leave the root admin in the list
    $output .= "<option value=\"1\">Admin</option>";
    foreach($users as $user)
    {
        $sel = ($post->post_author == $user->ID)?"selected='selected'":'';
        $output .= '<option value="'.$user->ID.'"'.$sel.'>'.$user->user_login.'</option>';
    }
    $output .= "</select>";

    return $output;
}
add_filter('wp_dropdown_users', 'switchAuthor');