如果你需要在后台某个页面添加上传按钮,你需要用到wp_enqueue_media()这个函数,用于引入上传所需组件。
1 |
add_action ( 'admin_enqueue_scripts', function () { if (is_admin ()) wp_enqueue_media (); } ); |
html代码例如:
1 |
<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代码例如:
1 |
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; }); } } }); |
1 |
$(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; }); |
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。