保存图像从URL到WordPress的媒体库
问题描述:
我有这从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,因为我想让它显示来自现场的图像。将其设置回本地上传文件夹位置开始正确显示图像。
发表了答案,因为别人提出了同样的问题。检查您的设置>媒体>文件的完整URL路径。
谢谢大家!
答
搜索并从谷歌下载插件..
这个插件“从URL导入”取代了内置的媒体 上传标签,让你从远程URL 下载图像成WordPress媒体库。 远程图像的缩略图在本地创建,上传后您可以添加自定义媒体属性(如标题,标题)和 将图片插入到您的博客文章中。 如何找到插件: 当编辑帖子时,点击“添加媒体” 然后选择“保存&从URL导入”:如果您的窗口是 大,这是在左边的边栏。如果您的窗口很小,您需要选择预先选择“插入 媒体”的下拉菜单。 功能列表: 直接将远程图像包含到博客文章中,无需 必须将其保存到您的计算机本地 成为更好的互联网公民:避免与 轻松连接图像!下载 与媒体库全面整合后 重命名图像 - 包括创建 缩略图..
像&份额
如果您确实使用url请求图片的路径是正确的,您是否有'.htaccess'文件?如果是,请将其内容发布在问题中。 –
使用media_sideload_image wordpress函数 – onlinewebsite
@onlinewebsite这和我用过的media_handle_sideload函数有什么区别? – Kyon147