SlideShare una empresa de Scribd logo
1 de 10
AJAX the WordPress way
●
    Justin Foell
●
    WordPress Developer @ 9seeds.com
●
    justin@9seeds.com
●
    @justinfoell
What is AJAX?
●
    Asynchronous JavaScript and XML
●
    A fast way of getting data or a portion of a
    page from the web server w/o having to do a
    full page request
How does AJAX work
            (in WordPress)?
●
    jQuery makes an AJAX call to the server from
    your browser
●
    WordPress routes the call appropriately
●
    The call is processed by WordPress core - or
    a theme or plugin - which returns HTML or
    JSON (JavaScript Object Notation) data
●
    Returned data is displayed appropriately
Required Pieces
●
    JavaScript (and form) on to trigger an AJAX
    call
●
    WordPress hook (wp_ajax_* action) to route
    the request
●
    A function to handle the request and return
    the expected result
Dirty Details
    Getting JavaScript/jQuery loaded
●
    Register & Enqueue your JavaScript using
    wp_register_script & wp_enqueue_script
●
    Remember to include 'jquery' as a dependency when
    registering your script, so it automagically gets loaded
●
    Use wp_localize_script to expose WordPress variables
    to JavaScript – such as the ajax submit URL (only
    needed for Front-End AJAX)
●
    AJAX calls always go to admin_url( 'admin-ajax.php' )
    regardless if they're made from the front-end or wp-
    admin
Dirty Details (cont.)
    JavaScript/jQuery AJAX call
●
    Use jQuery AJAX Shorthand Methods
●
    The data object should have a member
    variable called 'action' – the value of it will
    correspond to the WordPress action
    executed...
Dirty Details (cont.)
    WordPress ajax hook
    add_action( 'wp_ajax_<action-value>', 'function_to_run_on_ajax_call' );

●
    Should return valid HTML or JSON data
●
    You MUST exit or die!!!111one
WordPress Admin Example
●
    Almost the same as the front-end example
●
    Hook form into an existing page or add your
    form to a custom wp-admin page
●
    Register & Enqueue javascript
●
    ajaxurl is already set
●
    Use json_encode in WordPress
●
    Use jQuery.parseJSON in jQuery
Questions???
Thank you!
●
    Justin Foell – Developer @ 9seeds.com
●
    justin@9seeds.com
    Resources:
●
    api.jquery.com/category/ajax/shorthand-methods
●
    codex.wordpress.org/AJAX_in_Plugins
●
    Code: bit.ly/jf-wordup-2012
●
    Slides: bit.ly/jf-slides-wordup-2012

Más contenido relacionado

La actualidad más candente

Db migration automation
Db migration automationDb migration automation
Db migration automation
Thomas Queste
 
Introduction to angular js july 6th 2014
Introduction to angular js   july 6th 2014Introduction to angular js   july 6th 2014
Introduction to angular js july 6th 2014
Simona Clapan
 
Architecting single-page front-end apps
Architecting single-page front-end appsArchitecting single-page front-end apps
Architecting single-page front-end apps
Zohar Arad
 
Being a pimp without silverlight
Being a pimp without silverlightBeing a pimp without silverlight
Being a pimp without silverlight
Maarten Balliauw
 

La actualidad más candente (20)

Db migration automation
Db migration automationDb migration automation
Db migration automation
 
Introduction to angular js july 6th 2014
Introduction to angular js   july 6th 2014Introduction to angular js   july 6th 2014
Introduction to angular js july 6th 2014
 
Architecting single-page front-end apps
Architecting single-page front-end appsArchitecting single-page front-end apps
Architecting single-page front-end apps
 
React introduction
React introductionReact introduction
React introduction
 
Webinar: AngularJS and the WordPress REST API
Webinar: AngularJS and the WordPress REST APIWebinar: AngularJS and the WordPress REST API
Webinar: AngularJS and the WordPress REST API
 
Server rendering-talk
Server rendering-talkServer rendering-talk
Server rendering-talk
 
JavaScript straight from the Oracle Database
JavaScript straight from the Oracle DatabaseJavaScript straight from the Oracle Database
JavaScript straight from the Oracle Database
 
AngularJS for Java Developers
AngularJS for Java DevelopersAngularJS for Java Developers
AngularJS for Java Developers
 
Introduction To JavaScript Ajax
Introduction To JavaScript AjaxIntroduction To JavaScript Ajax
Introduction To JavaScript Ajax
 
Academy PRO: React JS. Redux & Tooling
Academy PRO: React JS. Redux & ToolingAcademy PRO: React JS. Redux & Tooling
Academy PRO: React JS. Redux & Tooling
 
Web Worker, Service Worker and Worklets
Web Worker, Service Worker and WorkletsWeb Worker, Service Worker and Worklets
Web Worker, Service Worker and Worklets
 
ClojureScript: I can't believe this is JavaScript
ClojureScript: I can't believe this is JavaScriptClojureScript: I can't believe this is JavaScript
ClojureScript: I can't believe this is JavaScript
 
Being a pimp without silverlight
Being a pimp without silverlightBeing a pimp without silverlight
Being a pimp without silverlight
 
ASP.NET Quick Wins - 20 Tips and Tricks To Shift Your Application into High Gear
ASP.NET Quick Wins - 20 Tips and Tricks To Shift Your Application into High GearASP.NET Quick Wins - 20 Tips and Tricks To Shift Your Application into High Gear
ASP.NET Quick Wins - 20 Tips and Tricks To Shift Your Application into High Gear
 
More efficient, usable web
More efficient, usable webMore efficient, usable web
More efficient, usable web
 
Web a Quebec - JS Debugging
Web a Quebec - JS DebuggingWeb a Quebec - JS Debugging
Web a Quebec - JS Debugging
 
MVC 1.0 als alternative Webtechnologie
MVC 1.0 als alternative WebtechnologieMVC 1.0 als alternative Webtechnologie
MVC 1.0 als alternative Webtechnologie
 
Making Single Page Applications (SPA) faster
Making Single Page Applications (SPA) faster Making Single Page Applications (SPA) faster
Making Single Page Applications (SPA) faster
 
Ajax assignment help
Ajax assignment helpAjax assignment help
Ajax assignment help
 
Introduction to ASP.NET MVC
Introduction to ASP.NET MVC Introduction to ASP.NET MVC
Introduction to ASP.NET MVC
 

Destacado

Module 8 Preparation
Module 8 PreparationModule 8 Preparation
Module 8 Preparation
schernick
 
Building your first WordPress plugin
Building your first WordPress pluginBuilding your first WordPress plugin
Building your first WordPress plugin
Justin Foell
 
เล่มจริง
เล่มจริงเล่มจริง
เล่มจริง
Krukook Sut
 
Module 7 Preparation
Module 7 PreparationModule 7 Preparation
Module 7 Preparation
schernick
 
Module 7 Interaction
Module 7 InteractionModule 7 Interaction
Module 7 Interaction
schernick
 
Module 6 Interaction
Module 6 InteractionModule 6 Interaction
Module 6 Interaction
schernick
 
Module 8 Interaction
Module 8 InteractionModule 8 Interaction
Module 8 Interaction
schernick
 
عرض بور بوينت عن اخلاقيات استخدام الانترنت وحماية المستخدمين
عرض بور بوينت عن اخلاقيات استخدام الانترنت وحماية المستخدمينعرض بور بوينت عن اخلاقيات استخدام الانترنت وحماية المستخدمين
عرض بور بوينت عن اخلاقيات استخدام الانترنت وحماية المستخدمين
MOKBAL
 
عرض بور بوينت عن اخلاقيات استخدام الانترنت وحماية المستخدمين
عرض بور بوينت عن اخلاقيات استخدام الانترنت وحماية المستخدمينعرض بور بوينت عن اخلاقيات استخدام الانترنت وحماية المستخدمين
عرض بور بوينت عن اخلاقيات استخدام الانترنت وحماية المستخدمين
MOKBAL
 
27 simple sentences
27 simple sentences27 simple sentences
27 simple sentences
vaibhav1996
 

Destacado (19)

Module 8 Preparation
Module 8 PreparationModule 8 Preparation
Module 8 Preparation
 
Building your first WordPress plugin
Building your first WordPress pluginBuilding your first WordPress plugin
Building your first WordPress plugin
 
เล่มจริง
เล่มจริงเล่มจริง
เล่มจริง
 
Where do I go from here?
Where do I go from here?Where do I go from here?
Where do I go from here?
 
Biofuels
BiofuelsBiofuels
Biofuels
 
Module 7 Preparation
Module 7 PreparationModule 7 Preparation
Module 7 Preparation
 
Naica crystalcavemexico
Naica crystalcavemexicoNaica crystalcavemexico
Naica crystalcavemexico
 
Module 7 Interaction
Module 7 InteractionModule 7 Interaction
Module 7 Interaction
 
Pimp your Gmail to get more done
Pimp your Gmail to get more donePimp your Gmail to get more done
Pimp your Gmail to get more done
 
Module 6 Interaction
Module 6 InteractionModule 6 Interaction
Module 6 Interaction
 
Module 8 Interaction
Module 8 InteractionModule 8 Interaction
Module 8 Interaction
 
Nginx [engine x] and you (and WordPress)
Nginx [engine x] and you (and WordPress)Nginx [engine x] and you (and WordPress)
Nginx [engine x] and you (and WordPress)
 
S peters final_dse
S peters final_dseS peters final_dse
S peters final_dse
 
New york city
New york cityNew york city
New york city
 
Customizing the WP Admin for fun and profit
Customizing the WP Admin for fun and profitCustomizing the WP Admin for fun and profit
Customizing the WP Admin for fun and profit
 
عرض بور بوينت عن اخلاقيات استخدام الانترنت وحماية المستخدمين
عرض بور بوينت عن اخلاقيات استخدام الانترنت وحماية المستخدمينعرض بور بوينت عن اخلاقيات استخدام الانترنت وحماية المستخدمين
عرض بور بوينت عن اخلاقيات استخدام الانترنت وحماية المستخدمين
 
Minimum Viable Product
Minimum Viable ProductMinimum Viable Product
Minimum Viable Product
 
عرض بور بوينت عن اخلاقيات استخدام الانترنت وحماية المستخدمين
عرض بور بوينت عن اخلاقيات استخدام الانترنت وحماية المستخدمينعرض بور بوينت عن اخلاقيات استخدام الانترنت وحماية المستخدمين
عرض بور بوينت عن اخلاقيات استخدام الانترنت وحماية المستخدمين
 
27 simple sentences
27 simple sentences27 simple sentences
27 simple sentences
 

Similar a AJAX the WordPress way

Jqeury ajax plugins
Jqeury ajax pluginsJqeury ajax plugins
Jqeury ajax plugins
Inbal Geffen
 
Jqeury ajax plugins
Jqeury ajax pluginsJqeury ajax plugins
Jqeury ajax plugins
Inbal Geffen
 

Similar a AJAX the WordPress way (20)

WCLA12 JavaScript
WCLA12 JavaScriptWCLA12 JavaScript
WCLA12 JavaScript
 
Ajax
AjaxAjax
Ajax
 
Ajax
AjaxAjax
Ajax
 
Ajax tutorial by bally chohan
Ajax tutorial by bally chohanAjax tutorial by bally chohan
Ajax tutorial by bally chohan
 
Ajax
AjaxAjax
Ajax
 
Ajax Tuturial
Ajax TuturialAjax Tuturial
Ajax Tuturial
 
Introduction to AngularJS For WordPress Developers
Introduction to AngularJS For WordPress DevelopersIntroduction to AngularJS For WordPress Developers
Introduction to AngularJS For WordPress Developers
 
Ajax
AjaxAjax
Ajax
 
1 ppt-ajax with-j_query
1 ppt-ajax with-j_query1 ppt-ajax with-j_query
1 ppt-ajax with-j_query
 
JavaScript & AJAX in WordPress
JavaScript & AJAX in WordPressJavaScript & AJAX in WordPress
JavaScript & AJAX in WordPress
 
Ajax3
Ajax3Ajax3
Ajax3
 
Jqeury ajax plugins
Jqeury ajax pluginsJqeury ajax plugins
Jqeury ajax plugins
 
Jqeury ajax plugins
Jqeury ajax pluginsJqeury ajax plugins
Jqeury ajax plugins
 
mukesh
mukeshmukesh
mukesh
 
Unobtrusive JavaScript
Unobtrusive JavaScriptUnobtrusive JavaScript
Unobtrusive JavaScript
 
WordPress and Ajax
WordPress and AjaxWordPress and Ajax
WordPress and Ajax
 
Ajax
AjaxAjax
Ajax
 
Ajax on drupal the right way - DrupalCamp Campinas, São Paulo, Brazil 2016
Ajax on drupal the right way - DrupalCamp Campinas, São Paulo, Brazil 2016Ajax on drupal the right way - DrupalCamp Campinas, São Paulo, Brazil 2016
Ajax on drupal the right way - DrupalCamp Campinas, São Paulo, Brazil 2016
 
How to make Ajax work for you
How to make Ajax work for youHow to make Ajax work for you
How to make Ajax work for you
 
Apache Wicket Web Framework
Apache Wicket Web FrameworkApache Wicket Web Framework
Apache Wicket Web Framework
 

Último

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

Último (20)

Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 

AJAX the WordPress way

  • 1. AJAX the WordPress way ● Justin Foell ● WordPress Developer @ 9seeds.com ● justin@9seeds.com ● @justinfoell
  • 2. What is AJAX? ● Asynchronous JavaScript and XML ● A fast way of getting data or a portion of a page from the web server w/o having to do a full page request
  • 3. How does AJAX work (in WordPress)? ● jQuery makes an AJAX call to the server from your browser ● WordPress routes the call appropriately ● The call is processed by WordPress core - or a theme or plugin - which returns HTML or JSON (JavaScript Object Notation) data ● Returned data is displayed appropriately
  • 4. Required Pieces ● JavaScript (and form) on to trigger an AJAX call ● WordPress hook (wp_ajax_* action) to route the request ● A function to handle the request and return the expected result
  • 5. Dirty Details Getting JavaScript/jQuery loaded ● Register & Enqueue your JavaScript using wp_register_script & wp_enqueue_script ● Remember to include 'jquery' as a dependency when registering your script, so it automagically gets loaded ● Use wp_localize_script to expose WordPress variables to JavaScript – such as the ajax submit URL (only needed for Front-End AJAX) ● AJAX calls always go to admin_url( 'admin-ajax.php' ) regardless if they're made from the front-end or wp- admin
  • 6. Dirty Details (cont.) JavaScript/jQuery AJAX call ● Use jQuery AJAX Shorthand Methods ● The data object should have a member variable called 'action' – the value of it will correspond to the WordPress action executed...
  • 7. Dirty Details (cont.) WordPress ajax hook add_action( 'wp_ajax_<action-value>', 'function_to_run_on_ajax_call' ); ● Should return valid HTML or JSON data ● You MUST exit or die!!!111one
  • 8. WordPress Admin Example ● Almost the same as the front-end example ● Hook form into an existing page or add your form to a custom wp-admin page ● Register & Enqueue javascript ● ajaxurl is already set ● Use json_encode in WordPress ● Use jQuery.parseJSON in jQuery
  • 10. Thank you! ● Justin Foell – Developer @ 9seeds.com ● justin@9seeds.com Resources: ● api.jquery.com/category/ajax/shorthand-methods ● codex.wordpress.org/AJAX_in_Plugins ● Code: bit.ly/jf-wordup-2012 ● Slides: bit.ly/jf-slides-wordup-2012