【问题标题】:Wordpress wp.media Upload imageWordpress wp.media 上传图片
【发布时间】:2016-01-24 12:05:31
【问题描述】:

好的,这是我的代码 - 标准代码,如果我只是选择现有图像就可以使用

 jQuery(function($){
 var file_frame;

 $('#upload-button').on( 'click', function( e ){
    e.preventDefault();
      // If the media frame already exists, reopen it.
if ( file_frame ) {
  file_frame.open();
  return;
}
 // Create a new media frame
file_frame = wp.media({
  title: 'Select or Upload Media Of Your Chosen Persuasion',
  button: {
    text: 'Use this media'
  },
  multiple: false  // Set to true to allow multiple files to be selected
});

 file_frame.on('open', function(){
     console.log("inside open");

 });
    file_frame.on('update', function(){
     console.log("inside update");
 });
file_frame.on('select', function(){

        // This will return the selected image from the Media Uploader, the result is an object
        var uploaded_image = file_frame.state().get('selection').first();
        // We convert uploaded_image to a JSON object to make accessing it easier
        // Output to the console uploaded_image
        console.log(uploaded_image);
        var image_url = uploaded_image.toJSON().url;
        // Let's assign the url value to the input field
        $('#image-url').val(image_url);
    });
    file_frame.open();
});
});

但是,如果我将图像拖放到其中,则图像已上传,但返回错误 “上传出错,请稍后重试。”

我错过了什么,我假设它对现有媒体选择窗口进行了某种刷新,而我的谷歌搜索没有出现任何内容。

干杯

编辑

我已经尝试了几种方法来让上传的图片在上传后显示在媒体框架中。没有太大的成功。 正如我所说,图像似乎已上传并添加到媒体库,但我错过了刷新或重新加载框架以显示图像。

任何人有经验或知道其他地方可以去看看吗?

【问题讨论】:

标签: php jquery wordpress plugins upload


【解决方案1】:

当一切都“完成”时,也许这个电话:

if(wp.media.frame.library) wp.media.frame.library.props.set({ignore: (+ new Date())});

或者如果您需要分离 库网格模式媒体上传(编辑帖子)模式

if(wp.media.frame.library){
    wp.media.frame.library.props.set({ignore: (+ new Date())});
  } else if(wp.media.frame.content.get().collection){
    wp.media.frame.content.get().collection.props.set({ignore: (+ new Date())});
    wp.media.frame.content.get().options.selection.reset();
}

【讨论】:

    猜你喜欢
    • 2015-07-17
    • 2014-12-27
    • 2011-01-28
    • 2023-03-23
    • 2017-11-29
    • 2019-08-01
    • 2023-03-16
    • 2013-10-26
    • 2018-06-13
    相关资源
    最近更新 更多