I am writing a plugin (github link) that allows the user to upload files in the admin-pages of wordpress.

I am running into an issue however: I created an upload form that essentially posts to itself and loads the contents of the result via AJAX (jQuery) inside the already loaded page.

The issue is that any content that is returned via URL of an admin page always has the footer of the page attached to it.

How can I return contents through an ajax call without those?

1 Answer 1


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.


function unc_gallery_upload_handler() {
    //handle request e.g. var_dump($_POST['data']);

add_action( 'wp_ajax_unc_gallery_upload', 'unc_gallery_upload_handler' );


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() {
    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).

  • Thanks, got it to work! Except that you need to use ` data {action: 'unc_uploads'}` instead, see here: github.com/uncovery/unc_gallery/blob/master/…
    – uncovery
    Feb 15, 2016 at 15:22
  • Yes, correct it should be a property on the data object, syntax correct. Glad got it working.
    – Adam
    Feb 16, 2016 at 3:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.