Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- add_action('wp_ajax_foobar_action', 'foobar_action');
- add_action('wp_ajax_nopriv_foobar_action', 'foobar_action');
- function foobar_action() {
- check_ajax_referrer();
- wp_send_json((object) ['msg' => 'hello world']);
- }
- add_action('admin_print_scripts', function () {
- printf('<script type="text/javascript">window.custom_nonce = "%s";</script>', wp_create_nonce());
- });
- var msg = '';
- // I'm using the whatwg-fetch polyfill and a polyfill for Promises.
- fetch(ajaxurl, {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8'
- },
- body: 'action=foobar_action&_wpnonce=' + window.custom_nonce
- }).then(function (res) {
- msg = res.json().msg;
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement