gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 05:13:05 25 4
gpt4 key购买 nike

我需要在 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 );
},

我在这个上传功能的底部添加了 alert('New image uploaded!'),浏览器提示'New image uploaded!'上传新图像时。但是我不想破解 WordPress 的核心,所以我想知道是否有一种方法可以在我的主题中编写一些代码来做同样的事情?对不起我的英语不好。谢谢大家的关注!

最佳答案

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

wp.Uploader.queue.on('reset', function() { 
alert('Upload Complete!');
});

我已经对其进行了测试,它适用于 WP 3.5 网站。

因此,这是完整版本,包括对“上传新媒体页面 上的常规 uploader 和新的 plupload uploader 的支持在“插入媒体对话框上。

创建一个名为:wp-admin-extender.js 的 javascript 文件并将其保存在您的 /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!');
});
}

最后;将此添加到主题的 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');

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

关于javascript - 如何在 WordPress 3.5 uploader 中上传新图像后立即运行一些代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14279786/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com