javascript WordPress前端媒体上传器

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript WordPress前端媒体上传器相关的知识,希望对你有一定的参考价值。

$('#media-button').click(function(e) {
  e.preventDefault();
  openMediaUploader();
});

function openMediaUploader() {

  // If the uploader object has already been created, reopen the dialog
  if ( mediaUploader ) {
    mediaUploader.open();
    return;
  }

  // Extend the wp.media object
  mediaUploader = wp.media.frames.file_frame = wp.media({
    title: 'Select image',
    button: {
      text: 'Select image'
    }, 
    library: {
      type: [ 'image' ]
    },
    multiple: false
  });

  // When a file is selected, grab the URL and set it as the text field's value
  mediaUploader.on('select', function() {
    attachment = mediaUploader.state().get('selection').first().toJSON();
    console.log(attachment.sizes);
    $( '#js-cover-image-url' ).val( attachment.url );
    $( '#js-cover-image-preview' ).attr( 'src', attachment.sizes.large.url );
  });

  // Open the uploader dialog
  mediaUploader.open();
}

以上是关于javascript WordPress前端媒体上传器的主要内容,如果未能解决你的问题,请参考以下文章

javascript WordPress媒体库JS片段

Wordpress 媒体上传器无法正确上传

javascript 适用于WordPress的Gulp设置代码段也可用于前端开发

更改 Wordpress 媒体图像 URL

媒体查询不适用于 Wordpress

Wordpress 媒体缩略图/固定链接