Advertisement
Guest User

Untitled

a guest
Jan 16th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. <?php
  2.  
  3. add_action('wp_ajax_foobar_action', 'foobar_action');
  4. add_action('wp_ajax_nopriv_foobar_action', 'foobar_action');
  5.  
  6. function foobar_action() {
  7. check_ajax_referrer();
  8.  
  9. wp_send_json((object) ['msg' => 'hello world']);
  10. }
  11.  
  12. add_action('admin_print_scripts', function () {
  13. printf('<script type="text/javascript">window.custom_nonce = "%s";</script>', wp_create_nonce());
  14. });
  15.  
  16. var msg = '';
  17.  
  18. // I'm using the whatwg-fetch polyfill and a polyfill for Promises.
  19. fetch(ajaxurl, {
  20. method: 'POST',
  21. headers: {
  22. 'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8'
  23. },
  24. body: 'action=foobar_action&_wpnonce=' + window.custom_nonce
  25. }).then(function (res) {
  26. msg = res.json().msg;
  27. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement