Support » Developing with WordPress » Adding additional roles to post author dropdown

  • Hi,

    On the sidebar of the gutenberg editor, there is a dropdown for the Author.

    By default this dropdown will only allow you to select users that has a role capable of creating posts, however I am looking to make it so you can select users within a specific role which does not currently have access to create articles.

    Any help that could be provided would be great.
    Regards,
    Ben

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    Use the ‘wp_dropdown_users_args’ filter to set the “who” element in the passed array to an empty string to get all users. ('author' or '' are the only accepted values) You can set or unset other args elements as desired to get different results. The args are those accepted by get_users() and WP_User_Query::prepare_query()
    https://developer.wordpress.org/reference/classes/wp_user_query/prepare_query/

    Thread Starter benjenk126

    (@benjenk126)

    Hi,

    I have copied a snippet from the WordPress site and it does not appear to work still

    function wpdocs_add_subscribers_to_dropdown( $query_args ) {
     
        $query_args['role__in'] = array( 'contributor', 'administrator', 'employer' );
     
        $query_args['role__not_in'] = array( 'editor' );
     
        unset( $query_args['who'] );
      
        return $query_args;
    }
    add_filter( 'wp_dropdown_users_args', 'wpdocs_add_subscribers_to_dropdown' );

    This is the code.
    Regards,
    ben

    Moderator bcworkz

    (@bcworkz)

    Hmmm… Looks like the code works for the classic editor and quick edit dialog, but not the block editor. It’s apparently using a different function I’m unfamiliar with 🙁 However, I suspect whatever it is cycles back to instantiating a WP_User_Query class object. If so, the “pre_get_users” action (ref_array type) could be used to alter the query’s “query_vars” property, which is the array of arguments passed to the object’s constructor.

    All sorts of user queries use this class, so you should make efforts to only alter the author dropdown query and no others. The “who” argument/query_var having ‘author’ as a value is a good indicator. Checking the get_current_screen() return value would be good insurance.

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.