2017-05-30 4 views
0

Kann mir jemand sagen, wie kann ich das Image des S3-Buckets als ein Feature-Bild des WordPres-Beitrags festlegen?
Hier ist mein CodebeispielWie setze ich das hochgeladene Bild von AWS-S3 als Feature-Bild von WordPress-Post ein?

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

I $ Ergebnis einstellen wollen [ 'ObjectURL'] als mein Beitrag featur Bild. Dank

Antwort

0

Verwenden unten Haken,

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

Verwandte Themen