Skip to content

Instantly share code, notes, and snippets.

@smileBeda
Created July 27, 2023 03:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smileBeda/49d78c6779179c4f8fc69fe5ca6d2f48 to your computer and use it in GitHub Desktop.
Save smileBeda/49d78c6779179c4f8fc69fe5ca6d2f48 to your computer and use it in GitHub Desktop.
JS to fix the WP Delete User reassign dropdown
jQuery(document).ready(function($) {
// Hide the existing dropdown
$('select[name="reassign_user"]').hide();
// Create a new dropdown
var $dropdown = $('<select id="your-admin-select2-users"></select>');
$('select[name="reassign_user"]').after($dropdown);
// Initialize Select2 and AJAX
$dropdown.select2({
width: '300px',
ajax: {
url: ajaxurl, // This is a variable that WordPress has already defined for us
dataType: 'json',
delay: 250,
data: function (params) {
return {
action: 'ajax_get_users', // This is the name of the function in your functions.php or plugin file
search: params.term // This is the text that the user has typed
};
},
processResults: function (data) {
return {
results: data
};
},
cache: true
},
minimumInputLength: 2
});
// When a user is selected, update the value of the existing dropdown
$dropdown.on('select2:select', function (e) {
var data = e.params.data;
$('select[name="reassign_user"]').val(data.id);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment