Wordpress Ajax Search & Autosuggest Plugin
fetch(restUrl) .then(response => response.json()) .then(data => renderResults(data); ) .catch(error => console.error('Error:', error));
wp_enqueue_style( 'wp-ajax-search-css', plugin_dir_url( __FILE__ ) . 'style.css', [], '1.0.0' ); wordpress ajax search & autosuggest plugin
wp_reset_postdata();
We use "debouncing" to ensure the Ajax request only fires after the user stops typing for 300ms. This prevents firing a request for every single keystroke. fetch(restUrl)
// Select the input and results container const searchInput = document.querySelector('.wp-ajax-search-input'); const resultsContainer = document.querySelector('.wp-ajax-search-results'); fetch(restUrl) .then(response =>