SlideShare a Scribd company logo
NATE REIST
AJAX: USING JAVASCRIPT 

IN WORDPRESS
FOLLOW ME ON TWITTER @NATEREIST
NATE REIST AJAX: USING JAVASCRIPT IN WORDPRESS
WHAT WE’LL BE TALKING ABOUT
• What is AJAX and why should we use it?
• How can we use AJAX in plugins and themes?
• Code examples
• Issues, tips and tricks
NATE REIST AJAX: USING JAVASCRIPT IN WORDPRESS
SOME ASSUMPTIONS
• You know some PHP
• You know some JavaScript
• You know something about WordPress hooks and filters
• Even if you don’t I hope this makes you aware of what you can
do with WP_AJAX
INTRODUCTION
WHAT IS AJAX?
NATE REIST AJAX: USING JAVASCRIPT IN WORDPRESS
Stronger than dirt!
NATE REIST AJAX: USING JAVASCRIPT IN WORDPRESS
WHAT IS AJAX?
AJAX stands for Asynchronous JavaScript and XMLHttpRequests

To break that down…
• Asynchronous means to not be synchronous, or not happening
at the same time. The data retrieved is not loaded at the time of
page load.
• JavaScript will request data from somewhere ( our WordPress in
this case )
• The data is returned in XML format
NATE REIST AJAX: USING JAVASCRIPT IN WORDPRESS
HOLD UP!
I don’t want XML, I want JSON! JavaScript Object Notation is
easier, in my opinion, to work with. Sometimes this is called AJAJ
(Asynchronous JavaScript and JSON), but that is one in the same.
NATE REIST AJAX: USING JAVASCRIPT IN WORDPRESS
WHY USE AJAX?
Pros:
• It is fast: load lighter pages quicker. You can request only what you need.
• It allows for more interactivity.
• Takes the load off your web server if you can defer some processing to
the client side
!
Cons:
• It is client side, so there are a lot of other areas for things to go wrong.
Every different browser comes back into the ring as a contender.
• Another language or library or two to really learn.
NATE REIST AJAX: USING JAVASCRIPT IN WORDPRESS
COMMON USES OF AJAX
• Contact Forms
• Infinite Scroll
• WP Post edits in the admin
• WP Menu editor
PLUGINS / THEMES
HOW TO USE AJAX
NATE REIST AJAX: USING JAVASCRIPT IN WORDPRESS
NATE REIST AJAX: USING JAVASCRIPT IN WORDPRESS
GETTING HOOKED
To get started, you need to have WordPress listening for AJAX calls. This can
be done by adding an action to the wp_ajax_ or wp_ajax_nopriv_ hook. Do
this by adding your action name to the hook as part of the string.

!
For example:
!
!
!
Your JavaScript can then on that hook. Something like: 

when clicked request AJAX response.

!
Your function should return a valid response that JavaScript can interpret.
NATE REIST AJAX: USING JAVASCRIPT IN WORDPRESS
STEPS IN AN AJAX TRANSITION
1. WordPress / PHP renders your page to the browser
2. JavaScript starts running in the user’s web browser
3. JavaScript makes AJAX call back into WordPress application
4. WordPress and PHP runs the appropriate hook and functions returns a
response to the browser
5. JavaScript parses the response properly and updates the DOM
NATE REIST AJAX: USING JAVASCRIPT IN WORDPRESS
1. WORDPRESS / PHP RENDERS 

YOUR PAGE TO THE BROWSER
The normal load of your page to the browser, theme and plugins loaded,
content gets pulled from the database, scripts get enqueued, AJAX hooks
are populated, page gets built.
NATE REIST AJAX: USING JAVASCRIPT IN WORDPRESS
2. JAVASCRIPT STARTS RUNNING 

IN THE USER’S WEB BROWSER
This is when all of your JavaScript starts to take hold with event listeners,
etc.
NATE REIST AJAX: USING JAVASCRIPT IN WORDPRESS
3. JAVASCRIPT MAKES AJAX CALL 

BACK INTO WORDPRESS
On click, AJAX request
NATE REIST AJAX: USING JAVASCRIPT IN WORDPRESS
4. WORDPRESS RUNS APPROPRIATE 

HOOKS AND FUNCTIONS
…then returns a response to the browser.
NATE REIST AJAX: USING JAVASCRIPT IN WORDPRESS
5. JAVASCRIPT PARSES THE 

RESPONSE + UPDATES THE DOM
NATE REIST AJAX: USING JAVASCRIPT IN WORDPRESS
DEMO
Download and code along: https://github.com/natereist/wcgr-ajax-likes/
USING AJAX
ISSUES, TIPS + TRICKS
NATE REIST AJAX: USING JAVASCRIPT IN WORDPRESS
NATE REIST AJAX: USING JAVASCRIPT IN WORDPRESS
SOME TIPS
• Use wp_nonces to determine user intent and keep the proper response
returned, as well as a light security layer
• Things you can return
• JSON
• XML
• HTML
• strings
• integers
• Enqueue your scripts properly and localize data to your scripts to keep it
clean
• wp_ajax_ hook is great for protected interactions, where as
wp_ajax_nopriv_ is for anyone to access
NATE REIST AJAX: USING JAVASCRIPT IN WORDPRESS
POTENTIAL ISSUES
• If you get a 0 it usually means there is an error in how you have hooked
your functions or spelling. You could also be trying to access a private
hook while not logged in, or you didn’t pass some other security check.
• If you get an internal server error, something in your code is wrong.
Usually a parse error.
• JavaScript cannot handle the response, or a JavaScript error is usually bad
formatting of data. Headers already sent can be due to a PHP notice or
warning from PHP.
NATE REIST AJAX: USING JAVASCRIPT IN WORDPRESS
TRICKS
• You can update WP AJAX callbacks in development and see results
without refreshing your page.
• Use Chrome developer tools and Firebug, etc. to quickly view JSON
responses using the console.log function in development. Don’t put this
into production unless necessary.
• It is not supported by all browsers, but you shouldn’t be logging
debugging data to consumers anyway!
• You can return full HTML, if you were just going to replace a section of the
site with new content and don’t want to parse JSON or XML.
• There is a core class for passing back really clean and good XML data if
you prefer that: 

http://codex.wordpress.org/Class_Reference/WP_Ajax_Response
• wp_send_json is great because it will JSON encode things for you and
then exit the PHP WordPress application.
NATE REIST AJAX: USING JAVASCRIPT IN WORDPRESS
COOL USES
Some things that you could do to make more interactive WordPress
applications:

• Auto saving things like meta fields
• Full JavaScript framework based on applications like Ember, Angular or
Backbone
• Cool web components with things like Polymer or react.js
• Live updating of content, score/stock tickers
• Simple or complex interactive features like chat clients or JavaScript
games
NATE REIST AJAX: USING JAVASCRIPT IN WORDPRESS
IN CONCLUSION…
WP_AJAX is a great way to extend the functionality of your
WordPress plugins, themes, sites or applications.
With a focus more lately on JavaScript-based web applications and
speed and bandwidth being an issue, it is a great way to begin to
optimize performance and increase interactivity.
NATE REIST AJAX: USING JAVASCRIPT IN WORDPRESS
CONNECT WITH ME
MY COMPANY mindutopia.com
!
ON WORDPRESS profiles.wordpress.org/natereist
!
TWITTER @natereist

More Related Content

What's hot (20)

PDF
Coding with jetpack
Rich Collier
 
PPT
Scaling Your Web Application
Ketan Deshmukh
 
PPTX
Jaggery Introductory Webinar
Nuwan Bandara
 
PPTX
ASP.NET Quick Wins - 20 Tips and Tricks To Shift Your Application into High Gear
Kevin Griffin
 
PPTX
Optimizing your WordPress website
mwfordesigns
 
PDF
Seravo.com: WordPress Security 101
Seravo
 
PPT
Web Application Introduction
shaojung
 
PDF
Progressive Web Apps. What, why and how
Riza Fahmi
 
PDF
Modern Static Site with GatsbyJS
Riza Fahmi
 
PPTX
Ajax assignment help
john mayer
 
PPTX
Iteratee and stream with Play2 scala
Quentin Adam
 
DOCX
Weebly login
marvinmikkelson
 
PDF
ReactJS Workflows
Cem Arguvanlı
 
PPTX
JS digest. Decemebr 2017
ElifTech
 
PPTX
AngularJS + NancyFx + MongoDB = The best trio for ultimate SPA by Bojan Velja...
Bojan Veljanovski
 
PDF
Understanding XHProf: Pinpointing Why Your Site is Slow and How to Fix it - S...
Ezra Gildesgame
 
PDF
Getting Started Adobe Edge Code CC & Brackets
Teerasej Jiraphatchandej
 
PPTX
Presentation on Gatsby to SF Static Web Tech Meetup
Kyle Mathews
 
PDF
How to install wordpress
CBitss Technologies
 
KEY
Architecting single-page front-end apps
Zohar Arad
 
Coding with jetpack
Rich Collier
 
Scaling Your Web Application
Ketan Deshmukh
 
Jaggery Introductory Webinar
Nuwan Bandara
 
ASP.NET Quick Wins - 20 Tips and Tricks To Shift Your Application into High Gear
Kevin Griffin
 
Optimizing your WordPress website
mwfordesigns
 
Seravo.com: WordPress Security 101
Seravo
 
Web Application Introduction
shaojung
 
Progressive Web Apps. What, why and how
Riza Fahmi
 
Modern Static Site with GatsbyJS
Riza Fahmi
 
Ajax assignment help
john mayer
 
Iteratee and stream with Play2 scala
Quentin Adam
 
Weebly login
marvinmikkelson
 
ReactJS Workflows
Cem Arguvanlı
 
JS digest. Decemebr 2017
ElifTech
 
AngularJS + NancyFx + MongoDB = The best trio for ultimate SPA by Bojan Velja...
Bojan Veljanovski
 
Understanding XHProf: Pinpointing Why Your Site is Slow and How to Fix it - S...
Ezra Gildesgame
 
Getting Started Adobe Edge Code CC & Brackets
Teerasej Jiraphatchandej
 
Presentation on Gatsby to SF Static Web Tech Meetup
Kyle Mathews
 
How to install wordpress
CBitss Technologies
 
Architecting single-page front-end apps
Zohar Arad
 

Similar to Nate Reist WCGR WP AJAX presentation (20)

ODP
AJAX the WordPress way
Justin Foell
 
PDF
JavaScript & AJAX in WordPress
Igor Benić
 
PDF
Introduction to AJAX In WordPress
Caldera Labs
 
PPT
Ajax and PHP
John Coggeshall
 
PDF
Ajax
soumya
 
PPTX
Introduction to Plugin Programming, WordCamp Miami 2011
David Carr
 
PDF
How to Use AJAX in PHP and jQuery.pdf
semsem20021
 
PDF
1 ppt-ajax with-j_query
Fajar Baskoro
 
PDF
WordPress Café: Using WordPress as a Framework
Exove
 
PPT
Ajax
sujaykumar
 
PDF
AJAX vs. jQuery What Are The Differences.pdf
Laura Miller
 
PPT
Ajax: User Experience
petrov
 
PDF
WordCamp Wilmington 2017 WP-API Why?
Evan Mullins
 
PDF
Ajax
Usman Khan
 
PDF
Introduction to ajax
Nir Elbaz
 
PDF
WordCamp Birmingham 2016 - WP API, What is it good for? Absolutely Everything!
Evan Mullins
 
DOCX
Copy of ajax tutorial
Abhishek Kesharwani
 
AJAX the WordPress way
Justin Foell
 
JavaScript & AJAX in WordPress
Igor Benić
 
Introduction to AJAX In WordPress
Caldera Labs
 
Ajax and PHP
John Coggeshall
 
Ajax
soumya
 
Introduction to Plugin Programming, WordCamp Miami 2011
David Carr
 
How to Use AJAX in PHP and jQuery.pdf
semsem20021
 
1 ppt-ajax with-j_query
Fajar Baskoro
 
WordPress Café: Using WordPress as a Framework
Exove
 
AJAX vs. jQuery What Are The Differences.pdf
Laura Miller
 
Ajax: User Experience
petrov
 
WordCamp Wilmington 2017 WP-API Why?
Evan Mullins
 
Ajax
Usman Khan
 
Introduction to ajax
Nir Elbaz
 
WordCamp Birmingham 2016 - WP API, What is it good for? Absolutely Everything!
Evan Mullins
 
Copy of ajax tutorial
Abhishek Kesharwani
 
Ad

Recently uploaded (20)

PDF
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 
PDF
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
PDF
TheFutureIsDynamic-BoxLang witch Luis Majano.pdf
Ortus Solutions, Corp
 
PDF
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
PDF
IDM Crack with Internet Download Manager 6.42 Build 43 with Patch Latest 2025
bashirkhan333g
 
PDF
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
PPTX
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
PDF
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
PDF
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
PDF
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
 
PPTX
Agentic Automation: Build & Deploy Your First UiPath Agent
klpathrudu
 
PPTX
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
PDF
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 
PPTX
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
 
PPTX
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
Alexander Marshalov - How to use AI Assistants with your Monitoring system Q2...
VictoriaMetrics
 
PPTX
Homogeneity of Variance Test Options IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PPTX
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
PDF
4K Video Downloader Plus Pro Crack for MacOS New Download 2025
bashirkhan333g
 
PPTX
Finding Your License Details in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
TheFutureIsDynamic-BoxLang witch Luis Majano.pdf
Ortus Solutions, Corp
 
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
IDM Crack with Internet Download Manager 6.42 Build 43 with Patch Latest 2025
bashirkhan333g
 
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
 
Agentic Automation: Build & Deploy Your First UiPath Agent
klpathrudu
 
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
 
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Alexander Marshalov - How to use AI Assistants with your Monitoring system Q2...
VictoriaMetrics
 
Homogeneity of Variance Test Options IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
4K Video Downloader Plus Pro Crack for MacOS New Download 2025
bashirkhan333g
 
Finding Your License Details in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Ad

Nate Reist WCGR WP AJAX presentation

  • 1. NATE REIST AJAX: USING JAVASCRIPT 
 IN WORDPRESS FOLLOW ME ON TWITTER @NATEREIST
  • 2. NATE REIST AJAX: USING JAVASCRIPT IN WORDPRESS WHAT WE’LL BE TALKING ABOUT • What is AJAX and why should we use it? • How can we use AJAX in plugins and themes? • Code examples • Issues, tips and tricks
  • 3. NATE REIST AJAX: USING JAVASCRIPT IN WORDPRESS SOME ASSUMPTIONS • You know some PHP • You know some JavaScript • You know something about WordPress hooks and filters • Even if you don’t I hope this makes you aware of what you can do with WP_AJAX
  • 4. INTRODUCTION WHAT IS AJAX? NATE REIST AJAX: USING JAVASCRIPT IN WORDPRESS
  • 6. NATE REIST AJAX: USING JAVASCRIPT IN WORDPRESS WHAT IS AJAX? AJAX stands for Asynchronous JavaScript and XMLHttpRequests
 To break that down… • Asynchronous means to not be synchronous, or not happening at the same time. The data retrieved is not loaded at the time of page load. • JavaScript will request data from somewhere ( our WordPress in this case ) • The data is returned in XML format
  • 7. NATE REIST AJAX: USING JAVASCRIPT IN WORDPRESS HOLD UP! I don’t want XML, I want JSON! JavaScript Object Notation is easier, in my opinion, to work with. Sometimes this is called AJAJ (Asynchronous JavaScript and JSON), but that is one in the same.
  • 8. NATE REIST AJAX: USING JAVASCRIPT IN WORDPRESS WHY USE AJAX? Pros: • It is fast: load lighter pages quicker. You can request only what you need. • It allows for more interactivity. • Takes the load off your web server if you can defer some processing to the client side ! Cons: • It is client side, so there are a lot of other areas for things to go wrong. Every different browser comes back into the ring as a contender. • Another language or library or two to really learn.
  • 9. NATE REIST AJAX: USING JAVASCRIPT IN WORDPRESS COMMON USES OF AJAX • Contact Forms • Infinite Scroll • WP Post edits in the admin • WP Menu editor
  • 10. PLUGINS / THEMES HOW TO USE AJAX NATE REIST AJAX: USING JAVASCRIPT IN WORDPRESS
  • 11. NATE REIST AJAX: USING JAVASCRIPT IN WORDPRESS GETTING HOOKED To get started, you need to have WordPress listening for AJAX calls. This can be done by adding an action to the wp_ajax_ or wp_ajax_nopriv_ hook. Do this by adding your action name to the hook as part of the string.
 ! For example: ! ! ! Your JavaScript can then on that hook. Something like: 
 when clicked request AJAX response.
 ! Your function should return a valid response that JavaScript can interpret.
  • 12. NATE REIST AJAX: USING JAVASCRIPT IN WORDPRESS STEPS IN AN AJAX TRANSITION 1. WordPress / PHP renders your page to the browser 2. JavaScript starts running in the user’s web browser 3. JavaScript makes AJAX call back into WordPress application 4. WordPress and PHP runs the appropriate hook and functions returns a response to the browser 5. JavaScript parses the response properly and updates the DOM
  • 13. NATE REIST AJAX: USING JAVASCRIPT IN WORDPRESS 1. WORDPRESS / PHP RENDERS 
 YOUR PAGE TO THE BROWSER The normal load of your page to the browser, theme and plugins loaded, content gets pulled from the database, scripts get enqueued, AJAX hooks are populated, page gets built.
  • 14. NATE REIST AJAX: USING JAVASCRIPT IN WORDPRESS 2. JAVASCRIPT STARTS RUNNING 
 IN THE USER’S WEB BROWSER This is when all of your JavaScript starts to take hold with event listeners, etc.
  • 15. NATE REIST AJAX: USING JAVASCRIPT IN WORDPRESS 3. JAVASCRIPT MAKES AJAX CALL 
 BACK INTO WORDPRESS On click, AJAX request
  • 16. NATE REIST AJAX: USING JAVASCRIPT IN WORDPRESS 4. WORDPRESS RUNS APPROPRIATE 
 HOOKS AND FUNCTIONS …then returns a response to the browser.
  • 17. NATE REIST AJAX: USING JAVASCRIPT IN WORDPRESS 5. JAVASCRIPT PARSES THE 
 RESPONSE + UPDATES THE DOM
  • 18. NATE REIST AJAX: USING JAVASCRIPT IN WORDPRESS DEMO Download and code along: https://github.com/natereist/wcgr-ajax-likes/
  • 19. USING AJAX ISSUES, TIPS + TRICKS NATE REIST AJAX: USING JAVASCRIPT IN WORDPRESS
  • 20. NATE REIST AJAX: USING JAVASCRIPT IN WORDPRESS SOME TIPS • Use wp_nonces to determine user intent and keep the proper response returned, as well as a light security layer • Things you can return • JSON • XML • HTML • strings • integers • Enqueue your scripts properly and localize data to your scripts to keep it clean • wp_ajax_ hook is great for protected interactions, where as wp_ajax_nopriv_ is for anyone to access
  • 21. NATE REIST AJAX: USING JAVASCRIPT IN WORDPRESS POTENTIAL ISSUES • If you get a 0 it usually means there is an error in how you have hooked your functions or spelling. You could also be trying to access a private hook while not logged in, or you didn’t pass some other security check. • If you get an internal server error, something in your code is wrong. Usually a parse error. • JavaScript cannot handle the response, or a JavaScript error is usually bad formatting of data. Headers already sent can be due to a PHP notice or warning from PHP.
  • 22. NATE REIST AJAX: USING JAVASCRIPT IN WORDPRESS TRICKS • You can update WP AJAX callbacks in development and see results without refreshing your page. • Use Chrome developer tools and Firebug, etc. to quickly view JSON responses using the console.log function in development. Don’t put this into production unless necessary. • It is not supported by all browsers, but you shouldn’t be logging debugging data to consumers anyway! • You can return full HTML, if you were just going to replace a section of the site with new content and don’t want to parse JSON or XML. • There is a core class for passing back really clean and good XML data if you prefer that: 
 http://codex.wordpress.org/Class_Reference/WP_Ajax_Response • wp_send_json is great because it will JSON encode things for you and then exit the PHP WordPress application.
  • 23. NATE REIST AJAX: USING JAVASCRIPT IN WORDPRESS COOL USES Some things that you could do to make more interactive WordPress applications:
 • Auto saving things like meta fields • Full JavaScript framework based on applications like Ember, Angular or Backbone • Cool web components with things like Polymer or react.js • Live updating of content, score/stock tickers • Simple or complex interactive features like chat clients or JavaScript games
  • 24. NATE REIST AJAX: USING JAVASCRIPT IN WORDPRESS IN CONCLUSION… WP_AJAX is a great way to extend the functionality of your WordPress plugins, themes, sites or applications. With a focus more lately on JavaScript-based web applications and speed and bandwidth being an issue, it is a great way to begin to optimize performance and increase interactivity.
  • 25. NATE REIST AJAX: USING JAVASCRIPT IN WORDPRESS CONNECT WITH ME MY COMPANY mindutopia.com ! ON WORDPRESS profiles.wordpress.org/natereist ! TWITTER @natereist