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.

  1. helloworld1.txt – very simple and basic “Hello World” plugin.
  2. helloworld2.txt – script that will display the text “Hello World” using an AJAX request to get the text from the server.
  3. helloworld3.txt – script that displays “Hello World” text, and an “update” link that increments a counter via an AJAX http request.
  4. 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.
  5. 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!

IMG_4625

IMG_4724

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!

Tagged with:  
Share →

38 Responses to Make your WordPress plugin talk AJAX

  1. Viper007Bond (28 comments.) says:

    I really should use AJAX more often. It’s so easy with jQuery and WordPress. Thanks for the reminder! :)

  2. Ozh (4 comments.) says:

    Thanks for this Donncha. Very well put and simple examples to follow.

  3. Ryan (1 comments.) says:

    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.

  4. Marcel (1 comments.) says:

    Thank you Donncha,
    I wanted to make my plugin Lazyest Gallery talk AJAX but I needed some simple exampes to start with.

  5. Donncha (1707 comments.) says:

    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.

  6. TheChrisD (71 comments.) says:

    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? :)

  7. thecancerus (1 comments.) says:

    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

  8. Donncha (1707 comments.) says:

    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.

  9. Donncha (1707 comments.) says:

    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!

  10. Ryck (Ricardo González) (1 comments.) says:

    Gran artículo sobre como integrar AJAX en tus plugins para el wordpress: http://tinyurl.com/6pl6kf

  11. Richard (1 comments.) says:

    I wish I have time to practice on Ajax on WordPress. It’s cool.

  12. nitemayr (Kevin Wardrop) (1 comments.) says:

    Interesting: http://tinyurl.com/6pl6kf

  13. James (1 comments.) says:

    Awesome! Thanks for the notes.
    Been looking for some simple ones like these.
    =-)

  14. CaptSolo (Uldis Bojars) (1 comments.) says:

    cool – @donncha used VIM for his BarCamp Cork presentation “Make your WordPress plugin talk AJAX” :Dhttp://tinyurl.com/6pl6kf

  15. waseem (1 comments.) says:

    are its working with IE6 ? i think that IE6 have a lot of problems with IE6 .

  16. YourGoToGuy (David Burch) (1 comments.) says:

    Reading: “Make your WordPress plugin talk AJAX” ( http://tinyurl.com/6pl6kf )

  17. Eric says:

    Thanks for the tutorial. This is the first one that has made sense to me.

    I have a question regarding the calling of functions within functions…

    For the sake of this example, I am trying to call one of my custom functions from inside the get_randomcol() function. But when I do so, I get the error that my custom function has not been defined. I know it’s defined as I reference it elsewhere and even on the same page as your script runs.

    So is it possible to call non-WP functions from within get_randomcol() and how?

    Thanks,
    Eric

  18. Donncha (1707 comments.) says:

    Eric – sure it’s possible, but the file containing the function has to be included first, obviously. Check our code again and don’t give up!

  19. Eric says:

    Thanks. I got it figured out. My functions file wasn’t being included because of the way I had included it in my plugin. I’ve fixed that and now it works great.

  20. madhav says:

    Thanx dude its really very helpful.

  21. john@johnsjottings.com (1 comments.) says:

    Does anyone know of any examples for 2.7 for a plugin that works like the Quick Edit or Reply options in the edit comment section of the admin panel? I’m looking to add my own option to that screen that allows an inline form to pop up similar to those.

    Thanks

  22. [...] Make your WordPress plugin talk AJAX Examples d’utilisation des fonctions d’AJAX native à WordPress [...]

  23. [...] previously wrote about adding AJAX to WordPress plugins but Thaya has worked through a simple example that will work perfectly with WP Super Cache. [...]

  24. lorelleonwp (Lorelle on WordPress) (1 comments.) says:

    WP Tips: Make your WordPress plugin talk AJAX http://tinyurl.com/6pl6kf

  25. shermanhu (Sherman Hu) (1 comments.) says:

    WP Tips: Make your WordPress plugin talk AJAX http://tinyurl.com/6pl6kf (via @lorelleonwp)

  26. Bryan (1 comments.) says:

    Hi Donncha,

    I just tested your samples and works great! I was wondering if you can give an example where it gets data from the database? Is that possible?

    Will really appreciate if you can help me on this.

  27. Jürgen (1 comments.) says:

    You are MY HERO
    This is what I was searching for.
    Some simple excamples for simple plugins to build.
    Thank you
    Jürgen

  28. kathy says:

    thank you for sharing all these cool examples. when developing a plugin w/ ajax, do you have any sort of backup for people who have javascript disabled? cheers!

  29. [...] The areas we required to put on our advertisements should not be so small. Hence, dashboard widget may be a good choice. We have tried our best to learn a lot about how to make plugins for WordPress using tutorials from various websites. Actually, I have wasted a lot time in reading those materials before one day, I got a very helpful link from Mr. Pan Tianyi who is from team “hermos”. The link is: http://ocaoimh.ie/make-your-wordpress-plugin-talk-ajax/ [...]

  30. Bob (1 comments.) says:

    Thanks Dude! I was struggling to wrap my head around the hooks. Now my pages whip you with ajax noodles!

  31. Seth Carstens (1 comments.) says:

    Awesome! Thank you so much. This helped me learn how to use AJAX in our Classifeds project to create “chained fields” allowing you to choose a parent category, then AJAX created a new dropdown to allow “child categories” to then be chosen. Many Many thanks.

  32. [...] Worth noting also that there are 2 methods of getting the AJAX on your page – one is the way i describe in this article( using localise_script ) … the other is to put the ajax in the head ( using an init or some other add action ), as described on this page [...]

  33. jerome says:

    hi – seems like your .txt files are gone; is that for good? i would love to read what you wrote as it seems by reading the comments quite some clear explanations on wp + ajax
    thanks for your help

  34. Ciprian (1 comments.) says:

    Great examples. For me examples are worth a thousand words.
    Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Holy Shmoly! is Stephen Fry proof thanks to caching by WP Super Cache