К functions.php
add_action( 'wp_ajax_your_custom_function', 'custom_function_init' );
add_action( 'wp_ajax_nopriv_your_custom_function', 'custom_function_init' );
function custom_function_init() {
$fields = $_REQUEST;
//do whatever you want
echo 'return something';
die();
}
К 'footer.php'
<script>
var ajax_url = "<?php echo admin_url( 'admin-ajax.php' ); ?>";
</script>
и вашим вещам Ajax, где-нибудь в файле JS
function do_ajax(data) {
if( xhr != null ) {
xhr.abort();
xhr = null;
}
xhr = $.ajax({
url: ajax_url,
timeout: 3600,
type: "POST",
cache: false,
data: ({
action:'your_custom_function',
data:data
}),
beforeSend: function() {
},
success: function( data ) {
console.log(data)
},
error: function( jqXHR, textStatus, errorThrown ) {
console.log( 'The following error occured: ' + textStatus, errorThrown );
},
complete: function( jqXHR, textStatus ) {
}
});
}