Php 如何将AWS-S3上传的图片设置为WordPress post的功能图片?
有人能告诉我如何将S3存储桶的图像设置为WordPres帖子的功能图像吗?Php 如何将AWS-S3上传的图片设置为WordPress post的功能图片?,php,wordpress,amazon-web-services,post,amazon-s3,Php,Wordpress,Amazon Web Services,Post,Amazon S3,有人能告诉我如何将S3存储桶的图像设置为WordPres帖子的功能图像吗? 这是我的代码示例 $s3Client = new S3Client([ 'version' => 'latest', 'region' => 'us-east-1', 'credentials' => [ 'key' => 'My-Key', 'secret' => 'My-Secret', ], ]); try { /
这是我的代码示例
$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']设置为我的PostFeatur图像。
谢谢在钩子下面使用
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;
}