I want to create an Autocomplete function in WordPress. I want a search field from where username can be searched. I am using following JQuery UI.
<label>Users</label>
<input type="text" name="user_name" id="user-name" />
<?php
$get_arr_user = array('John', 'Rogers', 'Paul', 'Amanda', 'Peter');
?>
<script>
jQuery(document).ready(function($) {
var availableTags = <?php echo json_encode($get_arr_user); ?>;
$( "#user-name" ).autocomplete({
source: availableTags
});
});
</script>
My problem is that I am not able to get the list of Usernames in this format – array('John', 'Rogers', 'Paul', 'Amanda', 'Peter');
How do I get that?
The other answers are correct, but it’s possible to achive the same thing with less code using wp_list_pluck()
:
$users = get_users();
$user_names = wp_list_pluck( $users, 'display_name' );
wp_list_pluck()
used that way will get the display_name
field of all the users in an array without needing to do a loop.