wp admin - Filter dropdown in users.php "delete user" bulk edit screen

admin2025-06-04  3

I recently attempted to delete an administrator user. Using the bulk editor, I selected "delete" and was presented with the option to assign their content to another user. However, the dropdown contains thousands of users (subscribers, customers, etc), making it difficult to get to the correct admin user I'd like to reassign the content to.

As a workaround, I edited users.php directly and added 'role' => 'administrator', to the wp_dropdown_users() array. This accomplished what I needed, but I'd like to permanently customize this dropdown the correct way (custom users.php page or a hook/filter that returns administrator users only, from functions.php or an upgrade-safe method).

The full code for that edit is this (line 271 as of WP 5.0.3):

<ul style="list-style:none;">
  <li><label><input type="radio" id="delete_option0" name="delete_option" value="delete" />
  <?php _e('Delete all content.'); ?></label></li>
  <li><input type="radio" id="delete_option1" name="delete_option" value="reassign" />
  <?php echo '<label for="delete_option1">' . __( 'Attribute all content to:' ) . '</label> ';
  wp_dropdown_users( array(
    'name' => 'reassign_user',
    'exclude' => array_diff( $userids, array( $current_user->ID ) ),
    'show' => 'display_name_with_login',
    'role' => 'administrator',
  ) ); ?></li>
</ul>

After some hunting, I found some helpful articles on adding a users page or customizing the users dropdown (if you want to add that somewhere else), but I'm not confident that putting the two concepts together will overwrite the existing users.php dropdown (it may just add a different users page).

How would you suggest pulling this off (ie. filter the users dropdown on the assign content screen)?

Thanks in advance!

I recently attempted to delete an administrator user. Using the bulk editor, I selected "delete" and was presented with the option to assign their content to another user. However, the dropdown contains thousands of users (subscribers, customers, etc), making it difficult to get to the correct admin user I'd like to reassign the content to.

As a workaround, I edited users.php directly and added 'role' => 'administrator', to the wp_dropdown_users() array. This accomplished what I needed, but I'd like to permanently customize this dropdown the correct way (custom users.php page or a hook/filter that returns administrator users only, from functions.php or an upgrade-safe method).

The full code for that edit is this (line 271 as of WP 5.0.3):

<ul style="list-style:none;">
  <li><label><input type="radio" id="delete_option0" name="delete_option" value="delete" />
  <?php _e('Delete all content.'); ?></label></li>
  <li><input type="radio" id="delete_option1" name="delete_option" value="reassign" />
  <?php echo '<label for="delete_option1">' . __( 'Attribute all content to:' ) . '</label> ';
  wp_dropdown_users( array(
    'name' => 'reassign_user',
    'exclude' => array_diff( $userids, array( $current_user->ID ) ),
    'show' => 'display_name_with_login',
    'role' => 'administrator',
  ) ); ?></li>
</ul>

After some hunting, I found some helpful articles on adding a users page or customizing the users dropdown (if you want to add that somewhere else), but I'm not confident that putting the two concepts together will overwrite the existing users.php dropdown (it may just add a different users page).

How would you suggest pulling this off (ie. filter the users dropdown on the assign content screen)?

Thanks in advance!

Share Improve this question asked Jan 21, 2019 at 19:15 Steck Insights Web DesignSteck Insights Web Design 558 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748984711a315388.html

最新回复(0)