Skip to content
Advertisement

how to create a custom live search in WordPress from scratch

I am new in WordPress and I trying to create a live search using ajax, I want when a user presses a button a box appears, and search results show in that box. for doing this I created a folder in my theme folder called livesearch in that folder I put a file called search-live.php and the code in that file like this:

JavaScript

and in my functions.php file I create a function called GetMySearchResult like this:

JavaScript

and I send data to search-live.php using ajax and code Like this:

JavaScript

I already $_GET['search']; and everything it’s ok and I can see the result in my console but when I call GetMySearchResult($_GET['search']) it gives me a fatal error like this:


Fatal error: Uncaught Error: Call to undefined function GetMySearchResult() in C:xampphtdocswebwp-contentthemeswebrankolivesearchsearch-live.php:3 Stack trace: #0 {main} thrown in C:xampphtdocswebwp-contentthemeswebrankolivesearchsearch-live.php on line 3

what I missed or what I do wrong?

Advertisement

Answer

For creating live search just need send a GET request to wp-json no need to make a new query

just use this URL in ajax request:

JavaScript

So ajax code should be like this:

JavaScript

and the HTML search input should be like this:

JavaScript

you must turn off the autocomplete of input

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement