限制wp.media模式仅显示当前用户上传的文件

时间:2017-09-06 01:33:05

标签: javascript wordpress filter media-library

我有一个wordpress插件,我想创建一个自定义的wp.media模式窗口,供用户从前端上传文件。我只想显示当前用户上传的文件。我不知道从哪里开始过滤此输入。这是我目前的javascript wp.media函数。我能够将库限制为特定的文件类型,但我不知道如何按current_user进行过滤。

function open_media_window() {
var upload = "";
if (this.window === undefined) {

  if($(this).hasClass('bizimg-select')){
    upload = "bizimg";
  }
  if($(this).hasClass('vcard-file')){
    upload = "vcard";
  }

  if(upload=='bizimg'||upload==""){
    this.window = wp.media({
            title: 'Upload my Bizcard Bizimg',
            library: {type: 'image/*',
          uploadedTo : wp.media.view.settings.post.id},
            multiple: false,
            button: {text: 'Insert'}
        });
  }
  if((upload=='vcard')){
    this.window = wp.media({
            title: 'Upload my Bizcard vCard',
            library: {type: 'text/x-vcard'},
            multiple: false,
            button: {text: 'Insert'}
        });
  }

  var self = this; // Needed to retrieve our variable in the anonymous function below
  this.window.on('select', function() {
          var files = self.window.state().get('selection').toArray();
          var first = files[0].toJSON();
          wp.media.editor.insert('[myshortcode id="' + first.id + '"]');
          //populate selected-bizimg-file with file name
          if(upload=='bizimg'){
            //populate bizimg_post with post id
            $('#bizimg_post').val(first["id"]);
            if($('#selected-bizimg-file')){
              $('#selected-bizimg-file').text(first['filename']);
            }
            if('img.bizimg'){
              $('img.bizimg').attr("srcset", first['url']).attr("src", first['url']);
              $('.bizimg-div').removeClass("border-blue");
            }
          }
          //do the same for vcard
          if(upload=='vcard'){
            $('#vcard_post').val(first["id"]);
            if($('#selected-vcard-file')){
              $('#selected-vcard-file').text(first['filename']);
              $('#vcard-raw-textarea').load(first['url']);
              $('#vcard-upload').css('color','#df9e06');
              $('.remove-file').removeClass('daisy-hidden');
            }
          }

      });

 }
    this.window.open();
   return false;
   }

1 个答案:

答案 0 :(得分:0)

经过一段时间的挖掘,这很容易。我想将答案归功于前一个问题中回答这个问题的caldaswordpress show only media user has uploaded in wp_editor 。如果您认为这只是一个重复的问题,请举报。当我处理自定义wp媒体模式时,我认为这是无关的。