我正在编写一个自定义主题/插件,其中我需要通过编程将某些网页上的图像下载到上传文件夹,然后将其作为帖子的一部分插入。
因此,我能够通过编程找到图像url,然后我需要将它们保存到wp content下的upload文件夹中,但是该文件夹中有特定的WordPress文件夹结构,用于保存图像。
现在我的问题是,是否有WordPress API或函数或方法允许我从web下载图像并将其保存到uploads文件夹?如果是,那是什么。
否则,我应该如何保存这些图像?
到目前为止,我正在这样做
$filetype = wp_check_filetype(basename($image_file_name), null );
$upload_dir = wp_upload_dir();
$attachment = array(
\'guid\' => $upload_dir[\'url\'] . \'/\' . basename( $image_file_name ),
\'post_mime_type\' => $filetype[\'type\'],
\'post_title\' => preg_replace(\'/\\.[^.]+$/\', \'\', basename($image_file_name)),
\'post_content\' => \'\',
\'post_status\' => \'inherit\'
);
$attachment_id = wp_insert_attachment( $attachment, $image_file_name, $post_id );
$attachment_data = wp_generate_attachment_metadata( $attachment_id, $image_file_name );
wp_update_attachment_metadata( $attachment_id, $attachment_data );
set_post_thumbnail( $post_id, $attachment_id );
但是上面的代码给出了以下错误
imagejpeg(http://wscdn.bbc.co.uk/worldservice/assets/images/2013/07/21/130721173402_egypts_new_foreign_minister_fahmy_304x171_reuters-150x150.jpg): failed to open stream: HTTP wrapper does not support writeable connections in C:\\dev\\wordpress\\pterodactylus\\wp-includes\\class-wp-image-editor.php on line 334
经过进一步调查,错误似乎是由
$attachment_data = wp_generate_attachment_metadata( $attachment_id, $image_file_name );
在进一步调查之后
wp_insert_attachment()
声明
The file MUST be on the uploads directory
关于
$image_file_name
那么,如何下载图像并将其正确保存到帖子中呢?
非常感谢。
最合适的回答,由SO网友:divups 整理而成
最近,我不得不通过社交媒体流的夜间cron脚本来实现这一点$parent\\u id是要将图像附加到的帖子的id。
function uploadRemoteImageAndAttach($image_url, $parent_id){
$image = $image_url;
$get = wp_remote_get( $image );
$type = wp_remote_retrieve_header( $get, \'content-type\' );
if (!$type)
return false;
$mirror = wp_upload_bits( basename( $image ), \'\', wp_remote_retrieve_body( $get ) );
$attachment = array(
\'post_title\'=> basename( $image ),
\'post_mime_type\' => $type
);
$attach_id = wp_insert_attachment( $attachment, $mirror[\'file\'], $parent_id );
require_once(ABSPATH . \'wp-admin/includes/image.php\');
$attach_data = wp_generate_attachment_metadata( $attach_id, $mirror[\'file\'] );
wp_update_attachment_metadata( $attach_id, $attach_data );
return $attach_id;
}
例如:
uploadRemoteImageAndAttach(\'http://some-external-site.com/the-image.jpg\', 122);
SO网友:gmazzap
您没有发布用于获取和保存图像的代码,因此无法说出错误在哪里。
请尝试以下代码以获取并保存图像:
function my_grab_image($url = NULL, $name = NULL ) {
$url = stripslashes($url);
if ( ! filter_var($url, FILTER_VALIDATE_URL) ) return false;
if ( empty($name )) $name = basename($url);
$dir = wp_upload_dir();
$filename = wp_unique_filename( $uploads[\'path\'], $name, NULL );
$filetype = wp_check_filetype($filename, NULL );
if ( ! substr_count($filetype[\'type\'], \'image\') ) return false;
try {
$image = my_fetch_image($url);
if ( ! is_string($image) || empty($image) ) return false;
$save = file_put_contents( $dir[\'path\'] . "/" . $filename, $image );
if ( ! $save ) return false;
return $dir[\'path\'] . "/" . $filename;
} catch ( Exception $e ) {
// echo $e->getMessage(); // Is a good idea log this error
return false;
}
}
function my_fetch_image($url) {
if ( function_exists("curl_init") ) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$image = curl_exec($ch);
curl_close($ch);
return $image;
} elseif ( ini_get("allow_url_fopen") ) {
$image = file_get_contents($url, false);
return $image;
} else {
throw new Exception(\'curl is not available and fopen url is disallowed\');
}
}
然后简单地将这些函数与代码结合使用,如下所示:
$image_file_name = @my_grab_image(\'http://this.is.the.image.url/image.jpg\');
if ( $image_file_name && file_exists($image_file_name) ) {
// PUT HERE YOUR CODE
}
还请记住
must 包括wp admin/includes/image。php在您的函数代码中
wp_generate_attachment_metadata()
要工作,请参阅
Codex希望对您有所帮助,但请注意这里的所有代码not 已测试。
SO网友:shankshera
您可以使用此功能将图像远程上载到uploads文件夹,并将其设置为特色图像。
function set_remote_featured_image($file, $post_id) {
if ( ! empty($file) ) {
// Download file to temp location
$tmp = download_url( $file );
// Set variables for storage
// fix file filename for query strings
preg_match( \'/[^\\?]+\\.(jpe?g|jpe|gif|png)\\b/i\', $file, $matches );
$file_array[\'name\'] = basename($matches[0]);
$file_array[\'tmp_name\'] = $tmp;
// If error storing temporarily, unlink
if ( is_wp_error( $tmp ) ) {
@unlink($file_array[\'tmp_name\']);
$file_array[\'tmp_name\'] = \'\';
}
// do the validation and storage stuff
$id = media_handle_sideload( $file_array, $post_id, $desc );
// If error storing permanently, unlink
if ( is_wp_error($id) ) {
@unlink($file_array[\'tmp_name\']);
return $id;
}
set_post_thumbnail( $post_id, $id );
}
}
用法:
set_remote_featured_image("http://example.com/image.jpg", 1);