最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

user roles - How to force listing contributors in authors dropdown menu

programmeradmin3浏览0评论

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?

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?

Share Improve this question edited Jun 5, 2017 at 22:20 fuxia 107k38 gold badges255 silver badges459 bronze badges asked Jun 5, 2017 at 13:54 RiccardoRiccardo 9711 gold badge18 silver badges37 bronze badges 3
  • To which drop-down menu are you trying to add them? Can you provide a screenshot? – Morgan Estes Commented 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!!! – April Schmitt Commented Jan 31, 2022 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 Commented Feb 4, 2022 at 19:25
Add a comment  | 

2 Answers 2

Reset to default 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',
    ) );
}

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;
}
发布评论

评论列表(0)

  1. 暂无评论