What you want to do is define a callback function to handle your request and to do that you need to hook onto:
…where “(action)” is the name of your “action” parameter that needs to be supplied in your AJAX request (more on that in a moment).
First, define a callback function in your PHP logic, let’s use the example of unc_gallery_upload
.
In this case, “unc_gallery_upload” is the name of your action, so when defining your hook, you combine wp_ajax_
with unc_gallery_upload
, and you get wp_ajax_unc_gallery_upload
, that’s the name of your action hook.
PHP
function unc_gallery_upload_handler() {
//handle request e.g. var_dump($_POST['data']);
}
add_action( 'wp_ajax_unc_gallery_upload', 'unc_gallery_upload_handler' );
JavaScript
In your AJAX request (JS code)
var options = {
//this will post to example.com/wp-admin/admin-ajax.php
//the ajaxurl variable is already defined in the global
//scope of the WordPress dashboard
url: ajaxurl,
//your payload, accessible on $_POST['data']
data: {
//this name of your action as defined in your wp_ajax_(action)
//hook that will fire your
action: 'unc_gallery_upload',
payload: 'somedata'
},
target: '#targetLayer',
beforeSubmit: beforeSubmit,
uploadProgress: uploadProgress,
resetForm: true,
complete: complete
};
$('#uploadForm').submit(function() {
$(this).ajaxSubmit(options);
return false;
});
I strongly recommend reading: wp_ajax_(action)
Note: since you are in the admin the area, you do NOT want to define a wp_ajax_nopriv_(action)
.