2017-09-06 46 views
0

我有一個wordpress插件,我想創建一個自定義wp.media模式窗口供用戶從前端上傳文件。我只想顯示當前用戶上傳的文件。我不確定從哪裏開始過濾這個輸入。這是我目前的javascript wp.media功能。我可以將庫限制爲特定的文件類型,但我不知道如何通過current_user進行過濾。限制wp.media模式只顯示當前用戶上傳的文件

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; 
    } 

回答