wp-ajax-book

WORDPRESS & AJAX An in-depth guide on using Ajax with WordPress

How About a Little Ajax?
I’m rather biased when it comes to Ajax. One
of my first WordPress plugins is Ajax-based. As
soon as I started using Ajax, I fell in love with the
possibilities.
You see, Ajax is what helps achieve that “rich” Internet experience. Ajax helps eliminate unnecessary page loads, can streamline a user interface,
and can make a task that is cumbersome run
gracefully behind the scenes.
As with every piece of technology, Ajax can be
used for good or for bad. There are those that will
use and abuse Ajax (and they should be spanked
unmercilessly).

Table of Contents
Acknowledgments ……………………………………….11
About Ronald Huereca – Author 12
Credits and Thanks 12
Introduction ……………………………………………….15
How About a Little Ajax? 16
The Book’s Beginning 16
The Goal of the Book 17
Helpful Tools for the Journey 18
Online Resources 19
A Word of Warning 19
Chapter 1
What is Ajax? ……………………………………………..21
Chapter 2
Adding Scripts Properly to WordPress …………….27
So How Does wp_enqueue_script Work? 29
Handle 29
Src 29
Deps 30
Ver 31
In_footer 31
Great, I have wp_enqueue_script down. Now what? 32
Naming Your Handlers 35
Loading Scripts Conclusion 36
Chapter 3
Localizing Your Scripts …………………………………37
wp_localize_script 39
Handle 39
Object_name 39
l10n 40
wp_localize_script Example 40
Other Localization Techniques 42
The JSON Technique 42
A Custom Function 44
Localization Conclusion 45
Chapter 4
Adding Styles Properly to WordPress ………………47
How Does wp_enqueue_style Work? 48
Handle, Src, Deps, and Ver 49
Media 49
The wp_enqueue_style Hooks 50
The init Technique 51
Conditional Comments 53
Loading Styles Conclusion 54
Chapter 5
Page Detection in WordPress …………………………55
WordPress Conditionals 57
Loading Scripts on the Front-End 58
Loading Scripts on the Home Page 59
Loading Scripts on the Front Page 60
Loading Scripts on Posts or Pages 60
Loading Scripts on Comment Pages 61
Loading Scripts for Post Types 62
Loading Scripts for Custom Taxonomies 62
Loading Scripts for Detecting Shortcodes 62
Load Scripts for a Specific Platform 63
Conditionals Conclusion 63
Page Detection in the Admin Area 64
Page Detection for Specific Admin Pages 65
Page Detection for Menu Items 66
Page Detection Conclusion 68
Chapter 6
WordPress Loading Techniques ……………………..69
Overriding Styles (and Scripts) 71
Disabling Styles (and Scripts) 73
Loading Just Your Scripts 75
Creating Standalone Pages 77
Loading WordPress Manually Using wp-load 77
Loading WordPress Using a Template Redirect 79
Preventing Plugins From Loading 83
WordPress Loading Techniques Conclusion 86
Chapter 7
Properly Formatting jQuery for WordPress Use ..87
Namespacing 88
Public Properties/Functions 91
Private Variables/Functions 92
Including Your jQuery Script in Your Theme 95
Conclusion for Properly Formatting jQuery for WordPress 96
Chapter 8
Nonces and the Escape API …………………………..97
WordPress Nonces 98
Nonces and Forms 101
Nonces and URLs 104
Nonces and Ajax 106
The Escape API 107
Validating Numeric Inputs 108
Escaping HTML 109
Escaping Attributes 110
Escaping JavaScript 111
Escaping URLs111
Filtering HTML 112
Escape API Conclusion 114
Chapter 9
Sending Our First Ajax Request …………………..115
Set Up the PHP Class to Handle Back-end Operations 117
Setting up the Interface 119
Setting Up the JavaScript File 122
Setting up the Ajax Object 124
Finalizing the functions.php Class 126
The get_comments Method 126
Add in our JavaScript Files 127
Add in JavaScript Localization 128
Add in String Localization 131
Add in Query Variable Support 131
Finalizing the Ajax Request 133
Chapter 10
Processing Our First Ajax Request ………………..139
Securing Your Ajax Processor 140
Performing a Nonce Check 141
Server-Side Processing of Ajax Requests 143
Sending an Ajax Response 146
Client-Side Processing/Parsing 150
Parsing the XML Document Object 150
Processing the Data 151
The Output 156
Chapter 11
WordPress and Admin Ajax …………………………165
WordPress’ admin-ajax.php 166
Registering the Ajax Processor 168
Getting the Location of the Ajax Processor 170
Passing Data to the Ajax Processor 171
The wp_ajax Callback Method 173
Finalizing the Ajax Processor 173
Admin Ajax Conclusion 175
Chapter 12
Example 1: WP Grins Lite …………………………177
The WPGrins Class 181
The Constructor 182
add_scripts and add_scripts_frontend 187
get_js_vars 188
add_styles and add_styles_frontend 190
add_admin_pages 191
print_admin_page 192
ajax_print_grins 193
wp_grins 193
get_admin_options 195
save_admin_options 196
Our Template Tag 197
The Admin Panel (admin-panel.php) 198
The JavaScript File (wp-grins.js) 203
WP Grins Lite Conclusion 207
Chapter 13
Example 2: Static Random Posts …………………209
Creating the Static Random Posts Widget 212
static_random_posts 214
form 215
update 218
get_admin_options 219
save_admin_options 221
init 221
add_admin_pages 222
print_admin_page 222
add_post_scripts 223
get_js_vars 224
widget 225
get_posts 229
build_posts 231
print_posts 233
ajax_refresh_static_posts 234
The Admin Panel (admin-panel.php) 237
Updating the Options 238
The User Interface 242
The JavaScript File (static-random-posts.js)246
Static Random Posts Conclusion 253
Chapter 14
Example 3: Ajax Registration Form ……………..255
Creating the Ajax_Registration Class 257
rform_shortcode 260
post_save 263
has_shortcode 265
add_scripts 266
add_styles 267
The Script File (registration.js) 269
Capturing the Form Data 270
Building the Ajax Object 272
Parsing the Ajax Response 273
The Ajax Processor 280
Parsing the Passed Form Data280
Data Validation 282
Creating the User 287
Sending the Response 288
Ajax Registration Form Conclusion 289
Now You Begin Your Own Journey ………………291

The WordPress and Ajax e-book is a comprehensive view on using Ajax with WordPress. It covers Ajax like you’ve never seen before. The book contains three real-life examples that provide the rationale and logic behind coding decisions, the reasons for Ajax’s use, and the steps from inception to completion of the examples.

Leave a Comment

Scroll to Top