One way is by using the load-users.php
hook which is fired only on the wp-admin/users.php
page, and then use the wp_dropdown_users_args
filter to modify the parameters (e.g. the role
parameter) passed to wp_dropdown_users()
.
In addition, we check to make sure that the field name is reassign_user
which is what WordPress use in the users.php
file. This is to prevent modifying the parameters for other users dropdown on the same page.
add_action( 'load-users.php', function(){
// Make sure the "action" is "delete".
if ( 'delete' !== filter_input( INPUT_GET, 'action' ) ) {
return;
}
add_filter( 'wp_dropdown_users_args', function( $query_args, $args ){
if ( 'reassign_user' === $args['name'] ) {
$query_args['role'] = 'administrator';
}
return $query_args;
}, 10, 2 );
} );
You can check the wp_dropdown_users()
source code to see what are the $query_args
and $args
, their values, etc.