This article, aimed at plugin developers, describes how to add Ajax to a plugin. Before reading this article, you should be familiar with the following: Ajax - Overview of the technology Writing a Plugin - How to write a plugin Plugin API - Filters and actions - what they are and how to use them How to add HTML to the appropriate WordPress page, post, or screen -- for instance, if you want to add Ajax to administration screens you create, you will need to understand how to add administration menus to WordPress; if you want to add Ajax to the display of a single post, you'll need to figure out the right filters and actions to add HTML to that spot on viewer-facing blog screens.
Trang 1G E T T H E S L I D E S
H T T P : / / W W W I T S A N A N D E R S O N C O M / W C S E A P D F
Using Ajax in Your Plugins and
Themes
Trang 2About Me
Trang 3Ajax – “What Does it Mean?”
Synchronous Requests Asynchronous Requests
“Asynchronous JavaScript and XML”
Trang 4Simplify With jQuery
Trang 5jQuery Basics – No Conflict
Trang 6jQuery Basics – Ajax Response Types
evaluation
the page
Trang 7Ajax in WordPress – Queue Your Script
Use wp_enqueue_script() to add your script to a page
handle – Name of script, e.g “my-awesome-script”
src – Where your script can be found
Use get_template_directory_uri() in parent themes
Use get_stylesheet_directory_uri() in child themes
Use plugins_url( „script.js‟, FILE ) in plugins
deps – An array of scripts that this script relies on, by handle If you need jQuery, use something like “array( „jquery‟ )” Go to wp_enqueue_script Codex page for a list of
available dependencies
ver – The version of the script Useful for “cachebusting” when your plugin is updated
in_footer – Whether to place the script in the footer (rather than the header) Defaults to false
Trang 8Ajax in WordPress – Queue Your Script (cont‟d)
Examples
Trang 9Ajax in WordPress – Queue Your Script (cont‟d)
intelligently
conditional tags
Trang 10Ajax in WordPress – Making Requests
Admin Ajax
Use „ajaxurl‟ variable
Frontend Ajax
Use „wp_localize_script‟ function
Use „init‟ hook
Trang 11Ajax in WordPress – Making Requests
Admin Ajax
Make sure you check user permissions Any logged in user can make ajax requests
Trang 12Ajax in WordPress – Making Requests
Frontend Ajax – wp_localize_script
Trang 13Ajax in WordPress – Making Requests
Frontend Ajax – init
Trang 14Ajax in WordPress – Another Response Type
Another Response Type – json
Trang 15Simplify Your Ajax Handlers
Trang 16Simplify Your Ajax Handlers
Trang 17Simplify Your Ajax Handlers
Trang 18Example Plugin
Simple Post Ratings
Trang 19Questions?
Will Anderson
http://www.itsananderson.com/
@itsananderson will@itsananderson.com