Created
July 27, 2023 03:42
-
-
Save smileBeda/49d78c6779179c4f8fc69fe5ca6d2f48 to your computer and use it in GitHub Desktop.
JS to fix the WP Delete User reassign dropdown
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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