bcworkz

Ajax-example.php

Aug 10th, 2020
740
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.62 KB | None | 0 0
  1. <?php
  2. /*
  3. The Ajax examples at https://developer.wordpress.org/plugins/javascript/ajax/
  4. and the subsequent PHP page are incomplete, which prevents them from working
  5. correctly. My full version of the examples which have worked for me.
  6. */
  7.  
  8. // The HTML, added to a custom HTML block in a post
  9. ?>
  10. <form id="radioform">
  11. <table>
  12. <tbody>
  13. <tr>
  14. <td><input class="pref" checked="checked" name="book" type="radio" value="Sycamore Row">Sycamore Row</td>
  15. <td>John Grisham</td>
  16. </tr>
  17. <tr>
  18. <td><input class="pref" name="book" type="radio" value="Dark Witch">Dark Witch</td>
  19. <td>Nora Roberts</td>
  20. </tr>
  21. </tbody>
  22. </table>
  23. </form>
  24.  
  25. <?php
  26. /*
  27.  The relevant PHP in a plugin that I created. Does not include the comment header
  28.  required for any plugin's main file.
  29.  
  30.     Enqueue required scripts and prepare needed data
  31.     Script invoked by changing book selection in the above form
  32. */
  33. function my_enqueue($hook) {
  34.     wp_enqueue_script( 'ajax-script',
  35.         plugins_url( '/js/my-jquery.js', __FILE__ ),
  36.         array('jquery')
  37.     );
  38.     $author_nonce = wp_create_nonce( 'title_example' );
  39.     wp_localize_script( 'ajax-script', 'my_ajax_object', array(
  40.         'ajax_url' => admin_url( 'admin-ajax.php?my-test=hello' ),
  41.         'nonce' => $author_nonce,
  42.         'post_id' => get_queried_object_id(),
  43.     ));
  44. }
  45. add_action('wp_enqueue_scripts', 'my_enqueue');
  46.  
  47.  
  48. /* Handle an AJAX request */
  49. function my_ajax_handler() {
  50.     check_ajax_referer( 'title_example' );
  51.     update_user_meta( get_current_user_id(), 'title_preference', $_POST['title'] );
  52.     $args = array(
  53.         'tag' => $_POST['title'],
  54.     );
  55.     $the_query = new WP_Query( $args );
  56.  
  57.     // appends (0) to the passed title as the query normally fails to find anything
  58.     echo $_POST['title'].' ('.$the_query->post_count.') ';
  59.     wp_die();       // All ajax handlers should die when finished
  60. }
  61. add_action( 'wp_ajax_my_tag_count', 'my_ajax_handler' );
  62. add_action( 'wp_ajax_no_priv_my_tag_count', 'my_ajax_handler' );
  63.  
  64.  
  65. // Content of my plugin's /js/my-jquery.js file
  66. ?>
  67. jQuery(document).ready(function($) {           //noConflict wrapper
  68.  
  69.     $(".pref").change(function() {             //event
  70.  
  71.         var this2 = this;                      //use in callback
  72.         $.post(my_ajax_object.ajax_url, {         //POST request
  73.            _ajax_nonce: my_ajax_object.nonce,     //nonce
  74.             action: "my_tag_count",            //action
  75.             title: this.value                  //data
  76.         }, function(data) {                    //callback
  77.             this2.nextSibling.remove();        //remove current title
  78.             $(this2).after(data);              //insert server response
  79.         });
  80.     });
  81. });
  82.  
Add Comment
Please, Sign In to add comment