The edit_attachment WordPress PHP action fires once an existing attachment has been updated.
Usage
add_action('edit_attachment', 'your_custom_function'); function your_custom_function($post_id) { // your custom code here }
Parameters
- $post_id (int): Attachment ID.
More information
See WordPress Developer Resources: edit_attachment
Examples
Log attachment updates
Log every time an attachment is updated.
add_action('edit_attachment', 'log_attachment_update'); function log_attachment_update($post_id) { error_log("Attachment with ID {$post_id} has been updated."); }
Update attachment metadata
Automatically add a custom field to attachments when they are updated.
add_action('edit_attachment', 'update_attachment_metadata'); function update_attachment_metadata($post_id) { update_post_meta($post_id, 'custom_field', 'Custom value'); }
Regenerate attachment thumbnail
Regenerate the attachment thumbnail when an attachment is updated.
add_action('edit_attachment', 'regenerate_thumbnail'); function regenerate_thumbnail($post_id) { wp_update_attachment_metadata($post_id, wp_generate_attachment_metadata($post_id, get_attached_file($post_id))); }
Notify admin of attachment update
Send an email notification to the admin when an attachment is updated.
add_action('edit_attachment', 'notify_admin_attachment_update'); function notify_admin_attachment_update($post_id) { $admin_email = get_option('admin_email'); $subject = 'Attachment Updated'; $message = "Attachment with ID {$post_id} has been updated."; wp_mail($admin_email, $subject, $message); }
Add watermark to updated attachments
Add a watermark to images when they are updated.
add_action('edit_attachment', 'add_watermark_to_attachment'); function add_watermark_to_attachment($post_id) { // Check if the attachment is an image if (wp_attachment_is_image($post_id)) { // Path to the watermark image $watermark_path = '/path/to/watermark.png'; // Add watermark to the image apply_watermark_to_image($post_id, $watermark_path); } }