2014-10-30 50 views
3

我希望用戶點擊一個按鈕,打開媒體窗口,只有帖子上傳到可見的特定頁面。WordPress wp.media定製

然後我希望他們的選擇填充輸入字段。

現在我正在努力尋找框架「後」類型和「選擇」類型之間的中間地帶。

如果我將wp.media幀設置爲「發佈」,我可以看到上傳到帖子的圖片,但我無法插入輸入字段,如果我設置爲「選擇」,我無法將選項限制爲「上傳到帖子」。

var uploader; 

$j(document).on('click','input[name="uploader_submit"]',function(){ 

    var button  = $j(this); 
    var parent  = button.parents('.uploader'); 
    var input  = $j('input:text:first',parent); 

    /* Extend the wp.media object */ 

    uploader = wp.media.frames.file_frame = wp.media({ 
     title: "Choose Media", 
     button: { text: "Choose Media" }, 
     multiple: false, 
     library: { type: "image" }, 
     frame: "post" 
    }); 

    /* When a file is selected, grab the URL and set it as the text field's value */ 

    uploader.on('select',function(){    

     attachment = uploader.state().get('selection').first().toJSON(); 
     input.val(attachment.url); 

    }); 

    /* Open the uploader dialog */ 

    uploader.open(); 

    return false; 

}); 

回答

0

使用方法如下

/* Extend the wp.media object */ 

uploader = wp.media.frames.file_frame = wp.media({ 
    title: "Choose Media", 
    button: { text: "Choose Media" }, 
    multiple: false, 
    library: { type: "image" }, 
    frame: "post", 
    state: "insert" 

}); 

/* When a file is selected, grab the URL and set it as the text field's value */ 

uploader.on('insert',function(){