Chapter 9. JavaScript, jQuery, and AJAX

JavaScript is a major component of any modern web app. jQuery is a popular JavaScript library that makes doing many things with JavaScript a lot easier. One of the things that is easier to do with jQuery is AJAX calls. This chapter will not teach you how to code in JavaScript or jQuery. This chapter will, however, teach you how to properly integrate your JavaScript code into your WordPress app.

Note

The term “JavaScript,” when used in this chapter and throughout the book, refers to core JavaScript or any code written in JavaScript that runs in the client browser, including jQuery and AJAX calls done in jQuery.

What Is AJAX?

The term AJAX stands for “Asynchronous JavaScript and XML” and is a way to use JavaScript to query the server after a page has already loaded. Historically, XML data would be returned and then processed by the browser using more JavaScript. These days, we more typically send back JSON-encoded data or straight up HTML to be incorporated into the app. In this chapter, we will cover executing an AJAX call via the jQuery ajax() method and also through the new Heartbeat API for WordPress.

What Is JSON?

JSON stands for “JavaScript Object Notation” and is a machine and human-readable format for transmitting data. It is especially useful when working with JavaScript, since a properly encoded JSON statement will be evaluated by JavaScript with no extra processing. To work with JSON in PHP, we will use the json_encode and json_decode

Get Building Web Apps with WordPress now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.