Minimal AJAX in WordPress

Minimal AJAX in WordPress

David Nash code, php Leave a Comment

Here’s what it takes to create minimal AJAX in WordPress. First, let’s create a button: Then functions.php include a Javascript file called do_ajax.js. It has the dependencies jquery and wp-util: Also in functions.php, we set up a function to handle the AJAX call, and add the actions: Finally, in our do_ajax.js file: And then in your console you should see “Hello!”. That string is being sent from do_ajax.js to the server, and the server simply sends it back again. That’s it – we have minimal AJAX in WordPress up and running! This post was based on this StackOverflow answer, and …

Autocomplete with AJAX in WordPress

Autocomplete with AJAX in WordPress: 2019

David Nash code, tutorial, wordpress Leave a Comment

This is the second part of Create an autocomplete field in WordPress: 2019, which works well if you don’t have that many options. If you have less than 10 options, use a select element. Less than 100-1000 (depending), you don’t need AJAX. If you have more than 100-1000, you’ll probably need to use autocomplete with AJAX. This means the page will load faster, and we’ll limit the data to entries that more closely match what the user is looking for. We didn’t even need Javascript for the previous part. What we’re doing now is a bit more complex, so we’ll …

Create an auto-complete field in WordPress

David Nash wordpress 115 Comments

I’m working on a project that has a large database – about 14,000 business listings. It also has a page for a user to sign up and select which business they work at. 14,000 is way too many options for a SELECT (drop-down) field and I don’t want to put that much load on the MySQL database either. My solution: an auto-complete text field that reads from the WordPress database. I take it one step at a time, and make sure each step works correctly before starting the next step. Here are the steps I took to do it. Front-end JavaScript …