4

I am aware that contributors cannot publish posts, and that by design WP will not show contributors in the author dropdown list (this has been discussed here: Contributors missing from author dropdown), however I am looking for a way to force listing contributors in the dropdown menu, when an author will create content to allow him/her to start creating content for a given contributor.

Possible?

3
  • To which drop-down menu are you trying to add them? Can you provide a screenshot? Jun 5, 2017 at 23:14
  • I just tried both of the above suggestions. And experienced that if you only filter it will still not include users with a 'contributor' role. However, the first script that removes and adds a custom author meta box, works wonderfully. Thank you!!! Jan 31 at 22:28
  • @AprilSchmitt don't post replies/comments as new answers to the question, I've converted it into a comment. Remember this is not a message board/forum, and you shouldn't use the answer box to get around not being able to comment with 1 rep, upvote instead or gain reputation
    – Tom J Nowell
    Feb 4 at 19:25

2 Answers 2

3

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',
    ) );
}
3
  • Works like a charm! How can you tell when a core function may be hooked and replaced?
    – Riccardo
    Jun 6, 2017 at 8:08
  • Awesome, glad to hear! In this case, I knew that the core used the standard meta box API, so I inspected the HTML to get the handle, which I then searched (grepped) the core code for. Generally speaking, if you're not already familiar with the hooks or APIs, you can try searching the core for various strings or HTML then work your way through to see if a filter or hook is available. Jun 6, 2017 at 8:15
  • 1
    ............"Chapeau!"
    – Riccardo
    Jun 6, 2017 at 8:16
7

You can just use wp_dropdown_users_args filter instead of creating metabox

add_filter('wp_dropdown_users_args', 'display_administrators_and_subscribers_in_author_dropdown', 10, 2);
function display_administrators_and_subscribers_in_author_dropdown($query_args, $r)
{
    if (isset($r['name']) && $r['name'] === 'post_author_override') {
        if (isset($query_args['who'])) {
            unset($query_args['who']);
        }
        $query_args['role__in'] = array('administrator', 'subscriber');
    }
    return $query_args;
}
2
  • Is there a similar filter for the author dropdown in quick edit ?
    – mike23
    Apr 9, 2019 at 12:09
  • @mike23 quick edit is using the same function wp_dropdown_users so it should work for both edit/quick edit screen. You might need to check $r['name']==='post_author' instead of post_author_override since inline edit uses this name. Apr 29, 2019 at 9:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.