1

What I am trying to do is -

1) User clicks "Add Media"

2) Select an image

3) Image uploads finishes

4) Now it should insert into post automatically without pressing "Insert into post".

Anywhere to hook, and click the "Insert into post" using jquery.

Any clues ?

2
  • The code for the media library is not fun to work with - trust me. Commented Nov 1, 2014 at 22:30
  • you're right, I am looking for any hack though. Commented Nov 2, 2014 at 4:52

1 Answer 1

3

The hook for doing stuff after an upload is the reset event on wp.Uploader.queue triggered by the pluploader when it's finished (see line 249 in "wp-includes/js/plupload/wp-plupload.js"). Here's one hack to use this to do an auto-insert after an upload:

function wpse167143_admin_footer() {
?>
<script>
jQuery( document ).ready(function() {
    typeof wp.Uploader !== 'undefined' && wp.Uploader.queue.on( 'reset', function () {
        // From the primary toolbar (".media-toolbar-primary")
        // get the insert button view (".media-button-insert")
        // and execute its click (as specified in its options).
        wp.media.frame.toolbar.get('primary').get('insert').options.click();
    } );
});
</script>
<?php
}
add_action( 'admin_footer' , 'wpse167143_admin_footer' );
0

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.