Want more features on Pastebin? Sign Up, it's FREE!
Guest

Sample WordPress AJAX

By: bcworkz on Jan 26th, 2013  |  syntax: PHP  |  size: 2.60 KB  |  views: 118  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /**
  2.  * ------------------ PAGE 1 bcw.php ------------------
  3.  */
  4. <?php
  5. /**
  6.  * sets up AJAX capability
  7.  *
  8.  * enqueues the needed jQuery script and creates a local ajax object for the script.
  9.  * registers and enqueues the CSS for the AJAX capable elements
  10.  *
  11.  * @parm string current admin page $hook
  12.  */
  13. function bcw_enqueue($hook) {
  14.     if( 'edit-comments.php' != $hook)   // Only applies to comments list
  15.         return;
  16.         $email_nonce = wp_create_nonce( 'bcw_comment_email' );
  17.         wp_enqueue_script( 'ajax-script', plugins_url( '/js/bcw.js', __FILE__ ),
  18.                  array('jquery'));
  19.         // in javascript, array elements are accessed as ajax_object.ajaxurl,
  20.         //       ajax_object.email, etc.
  21.         wp_localize_script( 'ajax-script', 'ajax_object',
  22.                 array( 'ajaxurl' => admin_url( 'admin-ajax.php' ), 'email'=>$email_nonce )
  23.         );
  24. }
  25. add_action( 'admin_enqueue_scripts', 'bcw_enqueue' );
  26.  
  27. /**
  28.  * toggles email setting for comments
  29.  *
  30.  * no return - dies
  31.  */
  32. function bcw_toggle_email() {
  33.         $commentid = $_POST['commentid'];               // getting variables from ajax post
  34.         check_ajax_referer('bcw_comment_email');        // die if not correct referer  
  35.         if (current_user_can('moderate_comments')) {
  36.                 $reply = get_comment_meta( $commentid, 'bcw_email_reply', true );
  37.                 // prepare nonce for next request
  38.                 $email_nonce = wp_create_nonce( 'bcw_comment_email' );
  39.                 // Toggle the current setting
  40.                 if ($reply == 'Yes') {
  41.                         update_comment_meta($commentid, 'bcw_email_reply', 'x');
  42.                         echo 'x,' . $email_nonce;
  43.                 } else {
  44.                         update_comment_meta($commentid, 'bcw_email_reply', 'Yes');
  45.                         echo 'Yes,' . $email_nonce;
  46.                 }
  47.         } else echo '!,' . $email_nonce;
  48.         die(); // stop executing script
  49. }
  50. add_action( 'wp_ajax_email_action', 'bcw_toggle_email' ); // ajax for logged in users
  51. ?>
  52.  
  53.  
  54. /**
  55.  *  ------------- PAGE 2 bcw.js ------------------------
  56.  */
  57.  /**
  58.  * javascript for BCW customizations
  59.  */
  60. jQuery(function($){
  61.         // toggle email comment setting
  62.         $("td.email.column-email").click(function () {
  63.                 var td = this;
  64.                 $.post(ajax_object.ajaxurl, {           //POST an AJAX request
  65.                         action: "email_action",
  66.                         commentid: $(this).find("input.commentid").attr("value"),
  67.                         //This nonce can only be used this one time
  68.                         _ajax_nonce: ajax_object.email 
  69.                 }, function(data) {                     //Handle AJAX response
  70.                         //includes email setting plus hidden comment id field
  71.                         var child = $(td).html();
  72.                         data = data.split(',');
  73.                         //save new nonce from server for next request
  74.                         ajax_object.email = data[1];
  75.                         //replace current value with new and add on the hidden comment id field
  76.                         var sym = child.split('<');
  77.                         $(td).html(data[0] + '<' + sym[1]);
  78.                 });
  79.         });
  80.  
  81. })
clone this paste RAW Paste Data