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_ID int — 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

Group wizard

One session: this hook, then each related_hooks entry. Walk it and tell us what to change.

edit_attachment → add_attachment → wp_generate_attachment_metadata

functions.php — edit_attachment
functions.php
PHP edit_attachment · Hook 1 of 3 · Step 1 of 6

Practice the edit_attachment WordPress action hook in our interactive sandbox — a VS Code-style editor with a step-by-step edit_attachment tutorial, add_action exercises, and instant feedback. No local WordPress install required.