WordPress后台页面里添加上传按钮

文章目录

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

  1. add_action ( 'admin_enqueue_scripts', function () {
  2. if (is_admin ())
  3. wp_enqueue_media ();
  4. } );

html 代码:

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

js 代码:

  1. jQuery(document).ready(function() {
  2. var $ = jQuery;
  3. if ($('.set_custom_images').length > 0) {
  4. if ( typeof wp !== 'undefined' && wp.media && wp.media.editor) {
  5. $(document).on('click', '.set_custom_images', function(e) {
  6. e.preventDefault();
  7. var button = $(this);
  8. var id = button.prev();
  9. wp.media.editor.send.attachment = function(props, attachment) {
  10. id.val(attachment.url);
  11. };
  12. wp.media.editor.open(button);
  13. return false;
  14. });
  15. }
  16. }
  17. });
© 版权声明
THE END
喜欢就支持以下吧
点赞0赞赏
分享
相关推荐
  • 暂无相关文章
评论 抢沙发