Get File Object from wp.Uploader

I would like to hook into the wp.Uploader and get the FileList object containing the file to be uploaded. So far I've been able to extend the uploader using the code below, but I can't quite seem to find the "before upload" hook.

$.extend( wp.Uploader.prototype, {
  success : function( file_attachment ){
    console.log( file_attachment );
  }
});

Topic media-modal plupload uploads Wordpress javascript

Category Web


You can't hook it directly as wp.Uploader doesn't expose it but you can use its init() to hook its internal pluploader instance:

add_action( 'admin_print_footer_scripts', function () {
    ?>
    <script type="text/javascript">
    (function ($) {
        if (typeof wp.Uploader === 'function') {
            $.extend( wp.Uploader.prototype, {
                init : function() { // plupload 'PostInit'
                    this.uploader.bind('BeforeUpload', function(file) {
                        console.log('BeforeUpload file=%o', file);
                    });
                },
                success : function( file_attachment ) { // plupload 'FileUploaded'
                    console.log( file_attachment );
                }
            });
        }
    })(jQuery);
    </script>
    <?php
}, 100 );

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.