如何在WordPress 3.5上传器中上传新图像后立即运行一些代码

Alv*_*vin 6 javascript wordpress file-upload backbone.js wordpress-3.5

我需要在WordPress 3.5上传器中上传新图像后立即运行一些代码.这是wp-includes/js/media-views.js的代码(第529-540行)

    uploading: function( attachment ) {
        var content = this.frame.content;

        // If the uploader was selected, navigate to the browser.
        if ( 'upload' === content.mode() ) 
            this.frame.content.mode('browse');

        // If we're in a workflow that supports multiple attachments,
        // automatically select any uploading attachments.
        if ( this.get('multiple') )
            this.get('selection').add( attachment );
    },
Run Code Online (Sandbox Code Playgroud)

我在此上传功能的底部添加了警报('新图像已上传!'),并且浏览器提示"新图像已上传!" 上传新图片时 但是我不想破解WordPress的核心,所以我想知道是否有一种方法可以在我的主题中编写一些可以做同样事情的代码?对不起我的英语不好.谢谢你们的关注!

Onu*_*rım 10

这行 wp-plupload.js显示上传器队列将在完成时重置.所以你可以这样做:

wp.Uploader.queue.on('reset', function() { 
    alert('Upload Complete!');
});
Run Code Online (Sandbox Code Playgroud)

我测试了它,它适用于WP 3.5站点.

因此,这里是完整版,包括支持" 上传新媒体 " 页面上的常规上传器和" 插入媒体 " 对话框上的新加载器.

创建一个名为的javascript文件wp-admin-extender.js,并将其保存在您的/custom/js/文件夹或模板目录中的任何位置.

// Hack for "Upload New Media" Page (old uploader)

// Overriding the uploadSuccess function:
if (typeof uploadSuccess !== 'undefined') {
    // First backup the function into a new variable.
    var uploadSuccess_original = uploadSuccess;
    // The original uploadSuccess function with has two arguments: fileObj, serverData
    // So we globally declare and override the function with two arguments (argument names shouldn't matter)
    uploadSuccess = function(fileObj, serverData) 
    {
        // Fire the original procedure with the same arguments
        uploadSuccess_original(fileObj, serverData);
        // Execute whatever you want here:
        alert('Upload Complete!');
    }
}

// Hack for "Insert Media" Dialog (new plupload uploader)

// Hooking on the uploader queue (on reset):
if (typeof wp.Uploader !== 'undefined' && typeof wp.Uploader.queue !== 'undefined') {
    wp.Uploader.queue.on('reset', function() { 
        alert('Upload Complete!');
    });
}
Run Code Online (Sandbox Code Playgroud)

最后; 将此添加到主题的functions.php中以在WP Admin中获取此功能:

//You can also use other techniques to add/register the script for WP Admin.
function extend_admin_js() {
    wp_enqueue_script('wp-admin-extender.js', get_template_directory_uri().'/custom/js/wp-admin-extender.js', array('media-upload', 'swfupload', 'plupload'), false, true);
}
add_action('admin_enqueue_scripts', 'extend_admin_js');
Run Code Online (Sandbox Code Playgroud)

这可能不是合法的解决方案,但它至少是一种解决方法.