Skip to content

Instantly share code, notes, and snippets.

@arunbasillal
Last active October 17, 2022 06:20
Show Gist options
  • Save arunbasillal/39a57b5951e3a13e44542cd61d423508 to your computer and use it in GitHub Desktop.
Save arunbasillal/39a57b5951e3a13e44542cd61d423508 to your computer and use it in GitHub Desktop.
Update image attributes when alt text is updated.
/**
* Update image attributes when alt text is updated.
*
* Runs when image alt text is updated using update_post_meta and
* calls iaffpro_auto_image_attributes_pro() function to update image attributes.
*
* Settings from "Bulk Updater Settings" tab are used to update the attributes.
*
* @param int $meta_id ID of metadata entry to update.
* @param int $object_id Image ID.
* @param string $meta_key Metadata key. Will be '_wp_attachment_image_alt' for image alt text.
* @param mixed $meta_value Metadata value.
*
* @link https://developer.wordpress.org/reference/functions/update_metadata/
*/
function prefix_iap_update_attributes_when_alt_text_changes( $meta_id, $object_id, $meta_key, $meta_value ) {
// Proceed only when image alt text is being updated.
if ( $meta_key !== '_wp_attachment_image_alt' ) {
return;
}
// Return if Image Attributes Pro is disabled.
if ( ! function_exists( 'iaffpro_auto_image_attributes_pro' ) ) {
return;
}
// Get the object of the image form image ID.
$image_object = get_post( $object_id );
// Run the pro module and update image attributes.
iaffpro_auto_image_attributes_pro( $image_object, true );
}
add_action( 'updated_postmeta','prefix_iap_update_attributes_when_alt_text_changes', 10, 4 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment