WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm calling media-upload.php via a custom icon click inside the content editor and I would like to add a custom meta value for all images that are uploaded when the media-upload.php is called from my custom function.

For example, for each of the images that are uploaded, I want to insert a value into wp_postmeta of _customAttachment = 1 like so:

update_post_meta($post['ID'], '_customAttachment', true);

I know how I can pass the current post-id to the media-upload.php (via querystring parameters), but I have no idea how to attach my update_post_meta filter to the save/upload trigger in media-upload.php

Is there a filter for this?

share|improve this question
would you mind please sharing your full solution here? Inane been trying to find a slick way to create a custom attach file/image feature to the post edit screen which uses the default media library while forcing any new upload or attachment done from that specific custom post type screen to have custom metadata appended to the files post meta table... Please do share! – NetConstructor.com Nov 29 '10 at 18:45
up vote 5 down vote accepted

Yes, you can add fields, an example

function rt_image_attachment_fields_to_save($post, $attachment) {
    // $attachment part of the form $_POST ($_POST[attachments][postID])
        // $post['post_type'] == 'attachment'
    if( isset($attachment['rt-image-link']) ){
        // update_post_meta(postID, meta_key, meta_value);
        update_post_meta($post['ID'], '_rt-image-link', $attachment['rt-image-link']);
    return $post;
// now attach our function to the hook.
add_filter("attachment_fields_to_save", "rt_image_attachment_fields_to_save", null , 2);

see more on this post

share|improve this answer
I'm almost there, however, I want to add the custom meta "_imageTop" to every attachment thats uploaded. Why do you check for isset()? – Scott B Nov 19 '10 at 18:49
when empty this field, then you have an php warning, thats all – bueltge Nov 19 '10 at 19:04

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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