Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?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' ),
]
);
$.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.
});
<?php
public function get_custom_data() {
if ( ! check_ajax_referer( 'acme-security-nonce', 'security' ) ) {
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
You can’t perform that action at this time.