1

I'm developing a plugin which requires some files which the user must upload.

I've seen that some plugins (like WooCommerce) seem to use the internal WP uploader.

How do I use it to let wp handle file uploads admin-side?

2

have you search in other question? this is the jquery script to call media uploader if u click a element media button

var $j = jQuery.noConflict();
function call_media_library() {
    var media_uploader;
    $j(document).on('click', '.video_media_btn', function(e){
        e.preventDefault();
        var btn_uploader = $j(this);
        if (media_uploader) {
            media_uploader.open();
            return;
        }
        media_uploader = wp.media({ 
            title: 'Upload media',
            //you can change type of media 
            library : { type : 'video'},
            multiple: false
        }).on('select', function(e){
            // This will return the selected media from the Media Uploader, the result is an object
            var uploaded_media = media_uploader.state().get('selection').first();
            // Convert uploaded_media to a JSON object to make accessing it easier
            var media_url = uploaded_media.toJSON().url;
            // Assign the url value to the input field
            //this is the element where u will put the url, I give example for video
            btn_uploader.parent().closest('.input-group.input-group-xs').siblings( '.input-media-display' ).find('.mp4-media-src').attr("src", media_url);
            //this is to run video again after change
            btn_uploader.parent().closest('.input-group.input-group-xs').siblings( '.input-media-display' ).find('video')[0].load();
        }).open();
    });
}

and this is to call other requirement for display a wordpress media uploader , this is a php function

add_action( 'wp_enqueue_scripts', 'insert_script_to_my_plugin' );
function insert_script_to_my_plugin(){
    wp_enqueue_media();
}
2
  • That worked! BTW it was media_uploader.state().get('selection').first(); instead of media.state().get('selection').first(); on line 18 :) I also removed the library parameter, as I can accept all files with no restrictions. Thanks! – BackSlash Feb 26 '17 at 22:56
  • oh sory for that, i forgot to change, because it for another question. Yeah if u remove library u get all – Manyang Feb 27 '17 at 1:47

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.