This morning at BarCamp Cork I gave a short talk on how to add AJAX functionality to WordPress plugins.
Here are the example scripts I used during the talk. Rename the files to .php and install as you would normal WordPress plugins.
- helloworld1.txt – very simple and basic “Hello World” plugin.
- helloworld2.txt – script that will display the text “Hello World” using an AJAX request to get the text from the server.
- helloworld3.txt – script that displays “Hello World” text, and an “update” link that increments a counter via an AJAX http request.
- helloworld4.txt – script that displays a simple form. The form has one text box, a “Random” button and a “Save” button. Clicking the “Random” button makes a request to the server to get a random number. “Save” sends the number to the server.
- helloworld5.txt and rate.txt – “Rating” script to rate a post (Originally from here). Makes an AJAX request to the server with the rating. Server returns randomly generated stats on rating. Place rate.php in wp-content/
All scripts that operate in the Settings page in the Dashboard use a nonce for security and access admin-ajax.php when making AJAX requests. The wp_ajax_$POST[ 'action' ] hook is used to execute the actual code that does something useful.
The rating script uses wp_enqueue_script() to load the jQuery class.
If you’re an Irish O2 user, and use Twitter you might like to install the new Tweet Tweet WordPress plugin. Thanks to Enda who let me use his O2 account briefly, I created a plugin that sends Twitter sms notifications using the free O2.ie web texts. That means Meteor, Vodafone and O2 are now covered. Anyone want to contribute a plugin for 3?
PS. I was very proud that Automattic was able to sponsor BarCamp Cork, and hopefully most of the WordPress badges found good homes! Conor says 116 signed up for the event, with just over 100 showing up. That’s a great number to pull in for a Saturday tech event!
More photos can be found on pix.ie and flickr. Looks like Phil hasn’t uploaded any of his shots yet! Can’t wait to see what came of his afternoon photo session. I had to leave early and missed it unfortunately.
Discovered photos of my presentation. Yes, I used Vi (or Vim if you want to be pedantic) to give my talk!
I wish I have time to practice on Ajax on WordPress. It’s cool.



38 Comments
Viper007Bond (28 comments.) on November 2, 2008 at 8:24 am.
I really should use AJAX more often. It’s so easy with jQuery and WordPress. Thanks for the reminder!
Ozh (4 comments.) on November 2, 2008 at 10:55 am.
Thanks for this Donncha. Very well put and simple examples to follow.
Ryan (1 comments.) on November 2, 2008 at 11:08 am.
Thanks
I’ve been needing to learn a bit of AJAX for a while now so hopefully these will help break me out of the noob bracket.
Marcel (1 comments.) on November 2, 2008 at 11:43 am.
Thank you Donncha,
I wanted to make my plugin Lazyest Gallery talk AJAX but I needed some simple exampes to start with.
Donncha (1707 comments.) on November 2, 2008 at 1:06 pm.
Glad they’re a help. My Tweet Tweet plugin uses jQuery too and I found it was hard finding info on how to make AJAX requests while in the dashboard. The nonce stuff required a bit of digging into WordPress code too, but it’s fairly straight forward once you’ve done it once.
TheChrisD (71 comments.) on November 2, 2008 at 2:13 pm.
So is there anything else new in the latest version of Tweet Tweet? Or can I safely ignore the little “2″ next to my Plugins tab?
thecancerus (1 comments.) on November 2, 2008 at 4:01 pm.
I had written something similar in April, without using nounce would love to have your feedback on that Simplified AJAX For WordPress Plugin Developers Using Jquery
Donncha (1707 comments.) on November 2, 2008 at 8:53 pm.
Chris – O2 support is the main thing, but the AJAX transition effects when the plugin updates are changed a bit and a settings link is added too.
Donncha (1707 comments.) on November 2, 2008 at 9:02 pm.
thecancerus – excellent tutorial. Maybe update it with a section on using a nonce? The security implications of using AJAX without a nonce are severe. A hacker can craft a url and if they trick you into clicking on it they can do bad things to your blog!