最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

wordpress - How to change author list in Authors dropdown in edit page display all user role if user logged as admin type of rol

matteradmin3PV0评论

Author dropdown

I am using classic editor and in edit page herer is a 'Author' dropdown and I want to modify value of this edit page (post type = 'page') authors dropdown. I want to show all users in this author dropdown of in edit page only when user logged as Admin/Administrator role type.

Want to change the list of users in the Author select dropdown on the edit page for Admin/Administrator role type users. I tried but not working for post type page.

I have 3 admin role user and when i logged as one of admin role type user i want all users are show in this author dropdown so admin can set any type of user as page(post type = page) author. currently by default only editors names are displaying in this author dropdown and i want to show all users.

I tried this without check user role but is not working for me in edit page in worpdress.

add_filter( 'wp_dropdown_users_args', 'change_user_dropdown', 10, 2 );

function change_user_dropdown( $query_args, $r ){
    // get screen object
    $screen = get_current_screen();

    // list users whose role is e.g. 'Author' for 'post' post type
    if( $screen->post_type == 'page' ):
        $query_args['role'] = array('Author');

        // unset default role 
        unset( $query_args['who'] );
    endif;

    

    return $query_args;
}

Author dropdown

I am using classic editor and in edit page herer is a 'Author' dropdown and I want to modify value of this edit page (post type = 'page') authors dropdown. I want to show all users in this author dropdown of in edit page only when user logged as Admin/Administrator role type.

Want to change the list of users in the Author select dropdown on the edit page for Admin/Administrator role type users. I tried but not working for post type page.

I have 3 admin role user and when i logged as one of admin role type user i want all users are show in this author dropdown so admin can set any type of user as page(post type = page) author. currently by default only editors names are displaying in this author dropdown and i want to show all users.

I tried this without check user role but is not working for me in edit page in worpdress.

add_filter( 'wp_dropdown_users_args', 'change_user_dropdown', 10, 2 );

function change_user_dropdown( $query_args, $r ){
    // get screen object
    $screen = get_current_screen();

    // list users whose role is e.g. 'Author' for 'post' post type
    if( $screen->post_type == 'page' ):
        $query_args['role'] = array('Author');

        // unset default role 
        unset( $query_args['who'] );
    endif;

    

    return $query_args;
}
Share Improve this question edited Nov 20, 2024 at 4:33 Sunshine asked Nov 18, 2024 at 11:20 SunshineSunshine 32 bronze badges 2
  • what do you need in this dropdown is the user is not administrator ? – mmm Commented Nov 18, 2024 at 12:49
  • I have 3 admin role user and when i logged as one of admin role type user i want all users are show in this author dropdown so admin can set any type of user as page(post type page) author. currently by default admin can see editors name in this author dropdown. – Sunshine Commented Nov 19, 2024 at 4:04
Add a comment  | 

2 Answers 2

Reset to default 0
add_filter('wp_dropdown_users_args', function ($query_args, $r) {
if (current_user_can('administrator')) {
    unset($query_args['who']);
}
return $query_args;
}, 10, 2);

as you don't explain if there is something to change when non administrator users edit a page, here is my answer to change the author list when the connected user has the capability edit_others_pages. you can change it according to your needs.

note also that the filter wp_dropdown_users_args works only on classical editor (like we see it on your screenshot) and not on gutenberg editor.

add_filter("wp_dropdown_users_args", function ($query_args, $parsed_args) {
    
    // the capability "edit_others_pages" can be change to another linked at the reason of the customisation
    if (current_user_can("edit_others_pages")) {
        
        // remove the filter on capabilities to return all users
        unset($query_args["capability"]);
        
    }
    
    
    return $query_args;
    
}, 10, 2);

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far