我想使用 URL 中的外部图像作为帖子缩略图,但是 我不想下载该图像并将其存储在我的服务器中。
我在其他问题中找到了这段代码:
$filename = "thumbnail_".$post_id".jpg";
$upload_file = wp_upload_bits( $filename, null, @file_get_contents('http://example.com/image.jpg') );
if ( ! $upload_file['error'] ) {
$filename = $upload_file['file'];
$wp_filetype = wp_check_filetype($filename, null );
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_parent' => $post_id,
'post_title' => preg_replace( '/\.[^.]+$/', '', $filename ),
'post_content' => '',
'post_status' => 'inherit'
);
$attachment_id = wp_insert_attachment( $attachment, $filename, $post_id );
file_put_contents('attachment_id.txt', print_r($attachment_id, true));
if ( ! is_wp_error( $attachment_id ) ) {
require_once(ABSPATH . 'wp-admin/includes/image.php');
$attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename );
wp_update_attachment_metadata( $attachment_id, $attachment_data );
set_post_thumbnail( $post_id, $attachment_id );
}
}
它将图像下载到我的服务器,并在我的上传目录中复制图像文件(我不知道为什么)
但是我不想下载图片我该如何实现呢?
假设此图像只是特色图像的替代 - 您可以将图像源的 URL 存储在自定义字段中。
add_post_meta( $post_id, 'image', 'http://example.com/image.jpg' );
然后您可以在任何需要的地方检索此 URL。
我还没有测试过这个,但假设你总是想要相同的文件,这可能会帮助你开始:
$filepath = 'http://example.com/'; //Change this to the actual file path...
$filename = 'image.jpg'; //Change this to the actual file name...
$filetype = wp_check_filetype( basename( $filename ), null );
$attachment = array(
'guid' => $filepath . $filename,
'post_mime_type' => $filetype['type'],
'post_parent' => $post_id,
'post_title' => preg_replace( '/\.[^.]+$/', '', $filename ),
'post_content' => '',
'post_status' => 'inherit'
);
$attachment_id = wp_insert_attachment( $attachment, $filename, $post_id );
if( !is_wp_error( $attachment_id ) ) {
require_once(ABSPATH . 'wp-admin/includes/image.php');
$attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename );
wp_update_attachment_metadata( $attachment_id, $attachment_data );
set_post_thumbnail( $post_id, $attachment_id );
}
如果您想在帖子中附加 URL 图像,请使用此(我假设您有
$post_id
):
if ($post_id) {
$image_name = basename($post_thumbnail_url);
$name = explode('.', $image_name)[0];
$upload_dir = wp_upload_dir();
$upload_dir_path = $upload_dir['path'];
$image_name = basename($post_thumbnail_url);
$image_path = $upload_dir_path . '/' . $image_name;
$image_data = file_get_contents($post_thumbnail_url);
if (file_put_contents($image_path, $image_data) !== FALSE) {
$image_url = $upload_dir['url'] . '/' . $image_name;
}
$type = wp_check_filetype($post_thumbnail_url)['type'];
$attachment = array(
'guid' => $image_url,
'post_mime_type' => $type,
'post_status' => 'inherit',
'post_title' => $name,
'post_parent' => $post_id
);
$attachment_id = wp_insert_attachment($attachment, $image_path, $post_id);
if ($attachment_id) {
$attachment_data = wp_generate_attachment_metadata($attachment_id, $image_path);
wp_update_attachment_metadata($attachment_id, $attachment_data);
set_post_thumbnail($post_id, $attachment_id);
} else {
wp_send_json_error(array('message' => 'Error While generating post attachment'));
wp_die();
}
} else {
wp_send_json_error(array('message' => 'Error While importing Posts'));
wp_die();
}