
Action Hook since 2.0.0
edit_attachment
Fires when an attachment's edit form is saved in wp-admin
Description
edit_attachment fires specifically when the classic Media > Edit Media screen saves its fields (title, caption, alt text, description) — it's the admin-side counterpart to save_post for attachments.
When it runs
Admin only, inside the edit_attachment() handler in wp-admin, after the attachment post is updated.
Signature
do_action( 'edit_attachment', int $post_ID );Parameters
post_IDint — The ID of the attachment that was just updated.
Examples
Basic
add_action( 'edit_attachment', function( $post_ID ) {
error_log( 'Attachment updated: ' . $post_ID );
} );Log every attachment edit.
Real case
add_action( 'edit_attachment', function( $post_ID ) {
$alt = get_post_meta( $post_ID, '_wp_attachment_image_alt', true );
if ( '' === trim( $alt ) ) {
update_post_meta( $post_ID, '_needs_alt_text', 1 );
}
} );Flag attachments still missing alt text after an edit, for an accessibility audit.
Edge case
add_action( 'edit_attachment', function( $post_ID ) {
// Does NOT fire when editing media through the block editor's
// attachment details panel via REST — that goes through rest_insert_attachment instead.
} );This hook only covers the classic wp-admin edit form, not REST-based edits.
Common Use Cases
- Validate or enforce alt text on save
- Sync updated media metadata to an external system
- Log editorial changes to media fields
- Re-trigger custom taxonomy assignment after a manual edit
Common mistakes
- Expecting edit_attachment to fire for REST/block-editor media edits — it only fires from the classic admin edit-attachment form
Related hooks
FAQ
Does edit_attachment fire when I update media via the REST API?
No — that path fires rest_insert_attachment instead. edit_attachment is specific to the classic wp-admin edit form.
What’s different between edit_attachment and save_post here?
save_post also fires for attachments since they're a post type, but edit_attachment is scoped specifically to the dedicated media-edit admin screen.
Source: wp-admin/includes/post.php