有谁能告诉我如何将S3存储桶的图像设置为WordPres帖子的特征图像?
这是我的代码示例
$s3Client = new S3Client([
'version' => 'latest',
'region' => 'us-east-1',
'credentials' => [
'key' => 'My-Key',
'secret' => 'My-Secret',
],
]);
try {
// Upload data.
$result = $s3Client-> putObject(array(
'Bucket' => 'cdn.myWeb.com',
'Key' => 'sunburst.png',
'Body' => fopen($image_url, 'r+'),
'ACL' => 'public-read'
));
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => sanitize_file_name($filename),
'post_content' => '',
'post_status' => 'inherit'
);
// Insert the post into the database
$lastInsertedId = wp_insert_attachment($attachment,
$result['ObjectURL'], $post_id);
if($lastInsertedId){
require_once(ABSPATH.'wp-admin/includes/image.php');
$res2 = set_post_thumbnail($post_id, $lastInsertedId);
$featureImageAdded = true;
}
} catch (S3Exception $e) {
echo $e->getMessage()."\n";
}
我想将$ result ['ObjectURL']设置为我的帖子特征图像。 感谢
答案 0 :(得分:0)
使用下面的钩子,
add_filter( 'post_thumbnail_html', 'my_post_image_html', 10, 5 );
function my_post_image_html( $html, $post_id, $post_thumbnail_id, $size, $attr ) {
$upload_dir = wp_upload_dir();
$base_url = $upload_dir['baseurl'];
// Change the default upload directory to AWS Bucket link
$AWSBucket = 'http://s3.amazonaws.com/bucket';
$html = str_replace($base_url, $AWSBucket, $html);
return $html;
}
https://wordpress.stackexchange.com/questions/120326/modify-featured-image-path-to-amazon-s3