Here’s a solution that will remove the original author meta box and replace it with a similar, but customized version which includes users with the contributor
role.
The logic for adding/removing the author meta box is pulled directly from the core. The meta box display callback is also cloned from the core, but we use the role__in
parameter of wp_dropdown_users()
which lets us specify which roles we want to include within the dropdown.
/**
* Removes the original author meta box and replaces it
* with a customized version.
*/
add_action( 'add_meta_boxes', 'wpse_replace_post_author_meta_box' );
function wpse_replace_post_author_meta_box() {
$post_type = get_post_type();
$post_type_object = get_post_type_object( $post_type );
if ( post_type_supports( $post_type, 'author' ) ) {
if ( is_super_admin() || current_user_can( $post_type_object->cap->edit_others_posts ) ) {
remove_meta_box( 'authordiv', $post_type, 'core' );
add_meta_box( 'authordiv', __( 'Author', 'text-domain' ), 'wpse_post_author_meta_box', null, 'normal' );
}
}
}
/**
* Display form field with list of authors.
* Modified version of post_author_meta_box().
*
* @global int $user_ID
*
* @param object $post
*/
function wpse_post_author_meta_box( $post ) {
global $user_ID;
?>
<label class="screen-reader-text" for="post_author_override"><?php _e( 'Author', 'text-domain' ); ?></label>
<?php
wp_dropdown_users( array(
'role__in' => [ 'administrator', 'author', 'contributor' ], // Add desired roles here.
'name' => 'post_author_override',
'selected' => empty( $post->ID ) ? $user_ID : $post->post_author,
'include_selected' => true,
'show' => 'display_name_with_login',
) );
}