0

When files are uploaded, I would like the, to automatically get inserted, bypassing the need to click on the "Insert into post" button

I use WordPress Multisite 3.5

enter image description here

I tried the following, but it didn't work.

add_filter( 'flash_uploader', '_force_html_uploader' );

function _force_html_uploader( $flash ) {
    remove_action('post-html-upload-ui', 'media_upload_html_bypass' );
    return false;
}

add_action('media_upload_tabs', '_media_upload_auto_insert_js');

function _media_upload_auto_insert_js(){
    ?><script src="<?php bloginfo('stylesheet_directory'); ?>/js/upload.js"></script><?php
}

// uploads.js

jQuery(document).ready(function(){
    if(jQuery(".savesend input")){
        jQuery(".savesend input").click();
    }
});

I also tried the following, but it didn't work.

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' );

thank you so much!

1

That's the pre-3.5 media interface so I think you must be on 3.4.x? Anyway the first way should work as long as you're using the browser (non-multifile) upload... try changing

add_filter( 'flash_uploader', '_force_html_uploader' );

to

add_action( 'pre-html-upload-ui', '_force_html_uploader' );

and the "js/upload.js" (note the subdirectory - it's expected to be in a "js" subdirectory of your theme directory) to

jQuery(document).ready(function(){
    if (typeof 'switchUploader' === 'function') switchUploader(1);
    if(jQuery(".savesend input")){
        jQuery(".savesend input").click();
    }
});

Neither change is that important, they just ensure your using the browser upload - which is important. The (other) important thing is that "js/upload.js" gets loaded (you can check it's being loaded by inspecting the frame source).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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