Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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