Skip to content

Instantly share code, notes, and snippets.

@EricBusch
Created December 11, 2017 14:11
Show Gist options
  • Save EricBusch/04fcfbb64253d63ee2911cedf7570623 to your computer and use it in GitHub Desktop.
Save EricBusch/04fcfbb64253d63ee2911cedf7570623 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