Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Simple AJAX test 2018-05-22
  4. Author: Kevin Meredith
  5. Description: Pluggin to test client site AJAX request
  6. Version: 1
  7. */
  8.  
  9. // disable direct file access
  10. if ( ! defined( 'ABSPATH' ) ) {
  11. exit;
  12. }
  13.  
  14. function load_js() {
  15.  
  16. wp_enqueue_script('ajax_test', plugin_dir_url( __FILE__ ) . 'ajax_test.js', array('jquery'), null, true );
  17.  
  18. $test_nonce = wp_create_nonce('test_nonce');
  19. wp_localize_script(
  20. 'ajax_test',
  21. 'wbs_ajax_obj',
  22. array(
  23. 'ajax_url' => admin_url( 'admin-ajax.php' ),
  24. 'nonce' => $test_nonce,
  25. )
  26. );
  27. error_log('Load JS TEST SITE');
  28. // wp_localize_script is used to send an array of php values to javascript
  29. // it embeds values in the HTML so that be used by java script
  30. }
  31. add_action( 'wp_enqueue_scripts', 'load_js' );
  32.  
  33. function test_ajax_process_request() {
  34. error_log('test_ajax_process_request() has been called');
  35. // first check if data is being sent and that it is the data we want
  36. //check_ajax_referer( $action, $query_arg, $die )
  37. if ( check_ajax_referer( 'test_nonce', 'js_nonce', false) ) {
  38. // 1st parameter is the were the nonce is stored in the $_POST array or what the nonce from field name
  39. // 2nd parameter is the name of the nonce which is set up in wp_create_nonce()
  40. // 3rd parameter is die, if true and nonce fails they function will return false
  41. $ran = rand(0, 100);
  42. $return_string = 'string from JS: "'.$_POST["from_js"].'" random number from server: '.$ran.' nonce: '.$_POST["js_nonce"].' TEST SITE';
  43. error_log($return_string);
  44. echo $return_string;
  45. die();
  46. }else{
  47. $return_string = 'nonce fail (ajax_test.php)';
  48. echo ($return_string);
  49. error_log($return_string);
  50. die();
  51. }
  52. }
  53. add_action('wp_ajax_test_ajax', 'test_ajax_process_request');
  54. add_action('wp_ajax_nopriv_test_ajax', 'test_ajax_process_request');
  55.  
  56. var $ = jQuery.noConflict();
  57.  
  58. $( document ).ready(function() {
  59.  
  60. console.log('Nonce: '+wbs_ajax_obj.nonce+' from ajax_test.js');
  61.  
  62. $.ajax({
  63. type:"POST",
  64. url: wbs_ajax_obj.ajax_url, // 'https://lomokev.com/ajaxtest'
  65. data: {
  66. action: "test_ajax", // name of the wordpress hook that will trigger function so in this case wp_ajax_nopriv_test_ajax and wp_ajax_test_ajax'
  67. js_nonce: wbs_ajax_obj.nonce,
  68. from_js: 'value of from_js from ajax_test.js'
  69. },
  70. success:function(data){
  71. console.log(data);
  72. },
  73. error: function(errorThrown){
  74. console.log(Object.values(errorThrown));
  75. }
  76.  
  77. });
  78.  
  79. // if it's a 404 the error says "error"
  80. // but this says "Bad Request"
  81.  
  82. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement