How to get attachment id as soon as it is uploaded through media uploader in jquery?

Using jquery, I am trying to get image attachment id of all images requested to upload through media uploader on post edit page or upload.php.
Here is what I tried but this works after all images are uploaded:

if (typeof wp.Uploader !== 'undefined' && typeof wp.Uploader.queue !== 'undefined') {
    wp.Uploader.queue.on('reset', function() { 
        console.log('images uploaded!');
    });
}

How can achieve the same?
Thanks

1
1

You are close, you just need to hook into the add event instead of the reset event. (In case you did not know, these are standard events provided by Backbone collections. So familiarizing yourself with that will be helpful when developing things around stuff where WordPress employs Backbone.js.)

So basically you’d modify your code like this:

if (typeof wp.Uploader !== 'undefined' && typeof wp.Uploader.queue !== 'undefined') {
    wp.Uploader.queue.on('add', function() { 
        console.log('image uploaded!');
    });
}

Leave a Comment