2012-01-19 62 views
1

我有一個帖子類型,用於在wordpress中上傳橫幅。使用自定義字段名稱,圖像/視頻和網址。默認的媒體上傳工作正常上傳圖片/視頻,但我的問題是我無法獲得視頻基地網址到自定義字段。如何使用wordpress上傳媒體上傳視頻文件?

這是我的代碼,無法獲取視頻網址。它只給我視頻文件名稱。

window.send_to_editor = function(html) { 

    var imgurlar = html.match(/<img[^>]+src=\"([^\"]+)\"/); 
    var imgurl = imgurlar[1]; 

    //image 
    if(imgurl.length){ 
     jQuery('#wsp_media').val(imgurl); 
     jQuery('#preview-wsp-media').html('<img src="'+imgurl+'" style="max-width:30px; max-height:50px"/><input type="button" value="Remove" class="button" onclick="removeUspMedia()"/>'); 
    } 
    //video 
    else{ 
     var fileurl = jQuery(html); 
      //above "html" carries only video name if I click on "none" button in media library 

     //check if fileurl is a video ?? 
     var fName = jQuery(fileurl).attr('href'); 

     ext = fName.split('.').pop().toLowerCase(); 
     var validVideos = [<?php echo "'" . implode("','", explode(' ', $this->validVideos)) . "'"?>]; 
     if(jQuery.inArray(ext, validVideos) == -1){ 
      alert('invalid video file selected'); 
     }else{ 
      jQuery('#wsp_media').val(fName); 
      jQuery('#preview-wsp-media').html('<img src="<?php bloginfo('url')?>/wp-includes/images/crystal/video.png" style="max-width:30px; max-height:50px"/><input type="button" value="Remove" class="button" onclick="removeUspMedia()"/>'); 
     } 
    } 
    tb_remove(); 

} 

回答

1

我自己找到了解決方案。

在 「media_send_to_editor」 添加過濾器

add_filter('media_send_to_editor', 'media_editor', 1, 3); 

追加視頻網址在輸出HTML

function media_editor($html, $send_id, $attachment){ 
    //get the media's guid and append it to the html 
    $post = get_post($send_id); 
    $html .= '<media>'.$post->guid.'</media>'; 
    return $html; 
} 

獲得媒體的URL這樣

window.send_to_editor = function(html) { 
     ....... 
     ....... 
     var pathArray = html.match(/<media>(.*)<\/media>/); 
     var mediaUrl = pathArray != null && typeof pathArray[1] != 'undefined' ? pathArray[1] : ''; 
     jQuery('#wsp_media').val(mediaUrl); 
     ....... 
     ....... 
    }