Loading
0

如何在WordPress后台的某个页面里添加上传按钮

如果你需要在后台某个页面添加上传按钮,你需要用到wp_enqueue_media()这个函数,用于引入上传所需组件。

add_action ( 'admin_enqueue_scripts', function () {
if (is_admin ())
wp_enqueue_media ();
} );

html代码例如:

<p>
<input type="number" value="" class="regular-text process_custom_images" id="process_custom_images" name="" max="" min="1" step="1">
<button class="set_custom_images button">Set Image ID</button>
</p>

js代码例如:

jQuery(document).ready(function() {
var $ = jQuery;
if ($('.set_custom_images').length > 0) {
if ( typeof wp !== 'undefined' && wp.media && wp.media.editor) {
$(document).on('click', '.set_custom_images', function(e) {
e.preventDefault();
var button = $(this);
var id = button.prev();
wp.media.editor.send.attachment = function(props, attachment) {
id.val(attachment.url);
};
wp.media.editor.open(button);
return false;
});
}
}
});

模板兔优化后的代码:

$(document).on('click', '.erphp-add-file', function(e) {
e.preventDefault();
var button = $(this);
var id = button.prev();
var original_send = wp.media.editor.send.attachment;
wp.media.editor.send.attachment = function(props, attachment) {
if($.trim(id.val()) != ''){
id.val(id.val()+'\n'+attachment.url);
}else{
id.val(attachment.url); 
}
wp.media.editor.send.attachment = original_send; 
};
wp.media.editor.open(button);
return false;
});

特别声明:1、本站仅提供源码学习下载,使用者需具备一定的技术基础,源码费用仅为站长辛苦整理费,不代表源码自身价值也不包含任何服务,如需完美运营请到官方购买。如需搭建、二开、bug修复等服务需额外收费,如果源码侵犯了您的利益请联系客服处理! 2、用户必须遵守《计算机软件保护条例(2013修订)》第十七条:为了学习和研究软件内含的设计思想和原理,通过安装、显示、传输或者存储软件等方式使用软件的,可以不经软件著作权人许可,不向其支付报酬。鉴于此条例,用户从本平台下载的全部源码(软件)仅限学习研究,未经版权归属者授权不得商用,若因商用引起的版权纠纷,一切责任均由使用者自行承担,本平台所属公司及其雇员不承担任何法律责任。本站邮箱:weituiw@qq.com