如果你需要在后台某个页面添加上传按钮,你需要用到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; });
原文链接:https://www.dqzy.cn/2025/05/05/512.html,转载请注明出处。
1、本站所有源码资源(包括源代码、软件、学习资料等)仅供研究学习以及参考等合法使用,请勿用于商业用途以及违法使用。如本站不慎侵犯您的版权请联系我们,我们将及时处理,并撤下相关内容!
2、访问本站的用户必须明白,本站对所提供下载的软件和程序代码不拥有任何权利,其版权归该软件和程序代码的合法拥有者所有,请用户在下载使用前必须详细阅读并遵守软件作者的“使用许可协议”,本站仅仅是一个学习交流的平台。
3、如下载的压缩包需要解压密码,若无特殊说明,那么文件的解压密码则为:www.dqzy.cn。
4、点启资源网是一个免费且专业分享网站源码、图片素材、特效代码、教程文章、站长工具的平台。我们努力给站长提供好的资源!
评论0