I want to add numbering to each image when I insert them into the post. For example if I insert 3 images into a post, there will be:

  1. Image 1

  2. Image 2

  3. Image 3

I looked through the WordPress documentation, but I can't find which hook is triggered when inserting images into a post.

3 Answers 3


The add_attachment action is fired when the wp_insert_attachment() function is called to add an item to the media library. Images are added to posts after going into the media library first. Even when adding via the post editor, items are added to the library with wp_insert_attachment() then to the post.

add_action( 'add_attachment', function( $post_ID ) { 
    // Do Stuff

This will not allow you to number items by post, only by instance in the media library. Whether that works for your purposes, I cannot answer.


  • Thank you, this is all I want to know, I will figure out the rest
    – Noob Coder
    Apr 24, 2016 at 6:30
  • 1
    Note this hook will only fire one time after the attachment gets uploaded into the media library, it will not get fired in any further attaches into any post, for that use wp_media_attach_action instead.
    – n0099
    Jan 31, 2023 at 7:35

If you actually want to hook into the point when the author of a post inserts an image or other attachment into the Wordpress editor, use image_send_to_editor()

For example, if you wanted to wrap the image in a <div> or <figure> each time, you could do it like this:

function wrap_attachments($html, $id, $caption, $title, $align, $url) {
  $src  = wp_get_attachment_image_src( $id, $size, false );
  $output = "<div id='post-$id media-$id' class='align$align'>";
  $output .= "<img src='$src[0]' alt='$title' width='$src[1]' height='$src[2]' />";
  $output .= "</div>";
  return $output;
add_filter( 'image_send_to_editor', 'wrap_attachments', 10, 9 );

Additional explanation in this article.

  • fyi - alt = metadata alt, post_content = the description, post_excerpt = caption; using wp_get_attachment_image will generate the html inc srcset. image_send_to_editor is not available in Gutenberg (yet)
    – Sandra
    Jan 19, 2023 at 11:40

From 5.5.0, there's a new hook wp_media_attach_action will monitor the event that fires after any attachment gets attached or detached more easily:

add_action('wp_media_attach_action', function (string $action, int $attachment_id) {
    if ($action !== 'attach') return;
    // do your stuff

Due to at/detach an attachment is just changing the value of post_parent for its post, this hook still won't get fired after the author inserts the attachment into any post editor, for that you'll need image_send_to_editor, but this hook won't work when using gutenberg editor: https://github.com/danielbachhuber/gutenberg-migration-guide/blob/master/filter-image-send-to-editor.md https://github.com/WordPress/gutenberg/issues/8472

Even the image_send_to_editor still won't get fired when the author is not inserting attachments into the editor by selecting them from the media library, but just directly copying and pasting images or manually writing raw HTML <img> tags, if you must catch these cases, the hook post_updated might be your last resort.

  • Thanks for the update!
    – jdm2112
    Feb 1, 2023 at 16:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.