我有這從URL保存爲存儲圖像的功能,唯一的問題是這一切似乎很好地工作比其他圖像「無法找到」保存圖像從URL到WordPress的媒體庫
圖片和名稱都在媒體庫中,但圖片被打破,因爲當您通過url打開圖片時,它會顯示「未找到」
如果我錯過了某些東西,您能否告訴我一些問題。
function set_image_from_url($url) {
$tmp = download_url($url);
$file_array = array(
'name' => basename($url),
'tmp_name' => $tmp
);
/**
* Check for download errors
* if there are error unlink the temp file name
*/
if (is_wp_error($tmp)) {
@unlink($file_array[ 'tmp_name' ]);
return $tmp;
}
/**
* now we can actually use media_handle_sideload
* we pass it the file array of the file to handle
* and the post id of the post to attach it to
* $post_id can be set to '0' to not attach it to any particular post
*/
$post_id = '0';
$id = media_handle_sideload($file_array, $post_id);
/**
* We don't want to pass something to $id
* if there were upload errors.
* So this checks for errors
*/
if (is_wp_error($id)) {
@unlink($file_array['tmp_name']);
return $id;
}
/**
* No we can get the url of the sideloaded file
* $value now contains the file url in WordPress
* $id is the attachment id
*/
$value = wp_get_attachment_url($id);
// Now you can do something with $value (or $id)
return $id;
}
如果您確實使用url請求圖片的路徑是正確的,您是否有'.htaccess'文件?如果是,請將其內容發佈在問題中。 –
使用media_sideload_image wordpress函數 – onlinewebsite
@onlinewebsite這和我用過的media_handle_sideload函數有什麼區別? – Kyon147