使用默认的WordPress Uploader Inside插件

时间:2017-02-26 作者:BackSlash

我正在开发一个插件,它需要用户上传一些文件。

我已经看到一些插件(如WooCommerce)似乎使用内部WP上传程序。

我如何使用它让wp在管理端处理文件上传?

1 个回复
最合适的回答,由SO网友:Manyang 整理而成

你有没有搜索其他问题?这是一个jquery脚本,用于在单击元素媒体按钮时调用媒体上传程序

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();
    });
}
这是调用显示wordpress媒体上传器的其他要求,这是一个php函数

add_action( \'wp_enqueue_scripts\', \'insert_script_to_my_plugin\' );
function insert_script_to_my_plugin(){
    wp_enqueue_media();
}