I need to use Selectize in wordpress ajax.
I have a selecto tag, I want to search by entering words and display the results in a dropdown. It is also possible to select several items and delete them.
I tried to do this with the code below but only the output in console.log is displayed as an array.
<select id="variations" name="variations[]" class="form-control variation-input" multiple></select>
PHP Code:
add_action('wp_ajax_search_terms', function () {
$terms = get_terms([
'taxonomy' => 'pa_colors',
'hide_empty' => false,
'search' => $_POST['search']
]);
foreach ($terms as $term) {
$return[] = array($term->term_id, $term->name);
}
echo json_encode($return);
wp_die();
});
Js Code:
$('.variation-input').selectize({
plugins: ["clear_button","remove_button"],
valueField: 'id',
labelField: 'name',
searchField: 'name',
options: [],
delimiter: ',',
preload: true,
persist: false,
create: false,
load: function (query, callback) {
if (!query.length) return callback();
$.ajax({
// type: 'GET',
type: 'post',
url: sudJs.ajax_url,
dataType: 'json',
data: {
action: 'search_terms',
name: query,
},
error: function () {
console.log('error');
callback();
},
success: function (result) {
console.log(result);
callback(result);
}
});
},
});
Console.log Return array of terms, but not showing in selectize.