-
-
Save tommcfarlin/a53a5c982b9e6826369e2f37914ad77b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
wp_enqueue_script( | |
'acme-security', | |
$this->plugin_url . 'assets/js/plugin.js', | |
[ 'jquery' ], | |
false, | |
true | |
); | |
wp_localize_script( | |
'acme-security', | |
'acme_ajax_object', | |
[ | |
'ajax_url' => admin_url( 'admin-ajax.php' ), | |
'security' => wp_create_nonce( 'acme-security-nonce' ), | |
] | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.get( acme_ajax_object.ajax_url, { | |
action: 'get_custom_data', | |
security: acme_ajax_object.security | |
}, function( response ) { | |
if ( undefined !== response.success && false === response.success ) { | |
return; | |
} | |
// Parse your response here. | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
public function get_custom_data() { | |
if ( ! check_ajax_referer( 'acme-security-nonce', 'security', false ) ) { | |
wp_send_json_error( 'Invalid security token sent.' ); | |
wp_die(); | |
} | |
// The rest of the function that does actual work. | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Regarding this line:
Without setting the 3rd
$die
parameter tofalse
, thewp_send_json_error()
andwp_die()
calls are never made and the request just exits returning a 403.In order to return the error message, you'll need this:
Hope this helps!