如果你需要在后台某个页面添加上传按钮,你需要用到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; }); } } });
阅读全文
原文链接:http://haojidi.com/?blog=%e5%a6%82%e4%bd%95%e5%9c%a8wordpress%e5%90%8e%e5%8f%b0%e7%9a%84%e6%9f%90%e4%b8%aa%e9%a1%b5%e9%9d%a2%e9%87%8c%e6%b7%bb%e5%8a%a0%e4%b8%8a%e4%bc%a0%e6%8c%89%e9%92%ae,转载请注明出处,如有侵权请立即删除。
评论0