I have been hunting in GOOGLE for a long time. I want to add custom field whenever user uploads new image in page or post. Is there any hooks or filters that can be used to detect new image upload in WordPress.

I am new to WP and I want to have this done. Thanks in advance.

1 Answer 1


When an image is uploaded via the media editor the action hook add_attachment is triggered, with the ID of the attachment passed as the parameter. You can get full post data for the attachment using the ID to determine the ID of the post/page the image is attached to (stored in the post_parent property). Example code:

add_action( 'add_attachment', 'GoWP_wpse156258' );
function GoWP_wpse156258( $ID ) {
  $attachment = get_post( $ID );
  $post_id = $attachment->post_parent;
  update_post_meta( $post_id, 'your_meta_key', 'your_meta_value' );

Hope that helps!

  • Thank you karpstrucking, but when you create new post, how does the ID comes. Second thing is how to get ID of the image being uploaded. Jul 31, 2014 at 3:03

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.