2013-10-05 18 views
0

我創建了一個插件作爲第三方來使用wordpress網站在主站點上使用acomodate系統。所以情景是: 當用戶點擊提交系統時,它也將添加到WordPress的網站,它運作完美,沒有任何問題。但是當我試圖通過wp_insert_attachment設置功能的圖像它讓我給像使用wp_insert_attachment時的URL錯誤

http://xxxxx.com/wp-content/uploads/http://xxxxx.com/system/media/.../xx.jpg 

一個網址我想成爲的人只有http://xxxxx.com/system/media/.../xx.jpg保存爲特色的形象,是有可能這樣做? 這裏是我當前的腳本

if($pt == "pictures"){ 
     $filename_url = $_GET["dml_file"]; 
     $mime = wp_check_filetype($filename_url, null); 
     $data = array(
     'post_mime_type' => $mime['type'], 
     'post_title' => preg_replace('/\.[^.]+$/', '', basename($_GET["dml_file"])), 
     'post_content' => '', 
     'post_status' => 'inherit' 
     ); 
     $attachment_id = wp_insert_attachment($data, $filename_url, $pid); 
     update_post_meta($pid, $custom_field, $attachment_id); 
     }else{ 
     update_post_meta($pid, $custom_field, $_GET["dml_file"]); 
     } 

我曾嘗試使用file_get_contentsfile_put_contents創造WP安裝映像,但我不希望這樣。

該系統提交此:

http://user:[email protected]/wp-content/plugins/dml3rdparty/dmlsubmit.php?dml_sa‌ve=save&dml_file=http://xxx.xxx.xxx.xxx/dml/assets/media/Accreditation/download.d‌15bdf4e9e.jpg&dml_type=Print Quality Photos&dml_description=test|download.jpg&dml_status=publish 
+0

很明顯,你的'filename_url'需要進行修整。 – brasofilo

+0

修剪,你是什麼意思?我想擺脫http://xxxxx.com/wp-content/uploads/ part –

+0

*動詞:通過刪除不規則或不需要的部分,使(某物)整齊或具有所需的大小或形式*執行'var_dump ($ filename_url);死();'檢查出來。 – brasofilo

回答

0

wp_insert_attachmentdocumentation(我用粗體強調):

$文件名
(字符串)(可選)文件上的位置服務器。使用絕對路徑而不是文件的URI。 文件務必在上傳目錄上。見wp_upload_dir()
默認值:false

許多可能的是,你可以用media_handle_sideload()解決這個問題。

0

該函數獲得文件內容使用捲曲:

function my_file_get_contents($url){ 
    $options = array(
     CURLOPT_AUTOREFERER => true, 
     CURLOPT_HEADER => false, 
     CURLOPT_FOLLOWLOCATION => true, 
     CURLOPT_RETURNTRANSFER => true, 
     CURLOPT_ENCODING => '', 
     CURLOPT_CONNECTTIMEOUT => 120, 
     CURLOPT_TIMEOUT => 120, 
     CURLOPT_MAXREDIRS => 10 
    ); 
    $ch = curl_init($url); 
    curl_setopt_array($ch,$options); 
    $data = curl_exec($ch); 
    curl_close($ch); 
    return $data; 
} 

此功能使用上述獲得的圖像,將其保存到上傳文件夾,並將它作爲一個後一個功能的圖像:

function my_featured_image($image_url,$post_id){ 
    $upload_dir = wp_upload_dir(); 
    $image_data = my_file_get_contents($image_url); 
    $filename = strtok($image_url, '?'); 
    $filename = basename($filename); 
    if(wp_mkdir_p($upload_dir["path"])){ 
     $file = $upload_dir["path"]."/".$filename; 
    }else{ 
     $file = $upload_dir["basedir"]."/".$filename; 
    } 
    file_put_contents($file, $image_data); 
    $wp_filetype = wp_check_filetype($filename,null); 
    $post_author = get_post_field("post_author",$post_id); 
    $attachment = array(
     "post_author" => $post_author, 
     "post_mime_type" => $wp_filetype["type"], 
     "post_title" => sanitize_file_name($filename), 
     "post_content" => "", 
     "post_status" => "inherit" 
    ); 
    $attach_id = wp_insert_attachment($attachment,$file,$post_id); 
    require_once(ABSPATH."wp-admin/includes/image.php"); 
    $attach_data = wp_generate_attachment_metadata($attach_id,$file); 
    $res1 = wp_update_attachment_metadata($attach_id,$attach_data); 
    $res2 = set_post_thumbnail($post_id, $attach_id); 
} 

調用具有:

my_featured_image($image_url,$post_id);