Chapter 12. JavaScript and Ajax in WordPress

WHAT'S IN THIS CHAPTER?

  • Understanding jQuery and Ajax

  • Correctly loading JavaScript in WordPress

  • Adding scripts only when needed

  • Making interactive interfaces with Ajax in WordPress

  • Implementing security checks in your Ajax requests

JavaScript is principally a language used to code plain text script executed on the client side, that is, the browser. In this chapter, occurrences of the term "script" refer to JavaScript script.

This chapter offers a concise introduction to jQuery, a JavaScript library used by WordPress, and to Ajax. Then it focuses on the WordPress specifics, introducing the functions and concepts you need to know, and eventually you author plugins using JavaScript and Ajax.

JQUERY–A BRIEF INTRODUCTION

jQuery is a popular JavaScript framework: It is used by more than 40% of the top million sites followed by Quantcast (source: http://trends.builtwith.com/javascript) and, more specific to this book's subject, is used by WordPress. jQuery on its own deserves more than an entire book, so this will be only a short preamble.

Benefits of Using jQuery

What makes jQuery such a great library and the reasons why it comes with WordPress are among the following:

  • It is light: The minified and gzipped library is only 24kb.

  • It uses a quick and terse syntax for faster developing (the write-less-do-more library).

  • It is completely a cross browser: What you develop works the same in IE 6+, Firefox 2+, Safari 3+, Chrome, and Opera 9+.

  • It is CSS3-compliant: ...

Get Professional WordPress® Plugin Development 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.