Skip to content

Instantly share code, notes, and snippets.

@MrImranJaved
Forked from EricBusch/update-image-meta.php
Created April 21, 2022 09:08
Show Gist options
  • Save MrImranJaved/57b8301bb5dcae6988ab892f897b7ac1 to your computer and use it in GitHub Desktop.
Save MrImranJaved/57b8301bb5dcae6988ab892f897b7ac1 to your computer and use it in GitHub Desktop.
Update the attached image's description, caption & alt text with values from the product. [datafeedr][dfrps]
<?php
/**
* Update the attached image's description, caption &
* alt text with values from the product when the image is imported.
*
* @param integer $attachment_id Attachment ID of imported image.
* @param Dfrps_Image_Importer $image_importer
*/
function mycode_update_image_meta( $attachment_id, $image_importer ) {
$image_id = $attachment_id;
$image_description = $image_importer->post->post_content;
$image_caption = $image_importer->post->post_excerpt;
$image_alt_text = $image_importer->post->post_title;
/**
* Update Image Description (post_content) & Caption (post_excerpt)
*/
wp_update_post( array(
'ID' => $image_id,
'post_content' => $image_description,
'post_excerpt' => $image_caption,
) );
/**
* Update Image Alt Text
*/
update_post_meta( $image_id, '_wp_attachment_image_alt', $image_alt_text );
}
add_action( 'dfrps_image_imported_successfully', 'mycode_update_image_meta', 20, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment