2017-05-30 12 views
0

どのように教えてもらえますか?S3バケットの画像をWordPres投稿の画像として設定できますか?ここ
は私のコードサンプル、私は私のポストfeatur画像として$結果[ 'いるObjectURL']を設定したいAWS-S3アップロードした画像をWordPressの投稿画像に設定するには?

$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"; 
} 

です。フック以下 おかげ

答えて

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

関連する問題