Getting Details Of Uploaded Image

I’m using WordPress’ upload system (as explained here) on my plugin’s admin page.

I can upload an image and get this image’s URL. But i need attachment ID, image caption etc. How can i reach them?

Read More

I have an <form> . It has 3 <input>s .

<input type="text" value="" /> //This input will be filled, after user upload an image and click to `Insert to post` button. I can do it.
<input type="button" value="Click for upload image" />
<input type="hidden" value="" name="ID" /> //Attachment ID will come here automagically (with JavaScript, maybe?). I don't know how can i get it.

Related posts

Leave a Reply

3 comments

  1. The attachment metadata stored for the image is:

    Array
    (
        [width] => 558
        [height] => 771 
        [hwstring_small] => height='62' width='128'
        [file] => 2012/02/logo2.png
        [sizes] => Array
        (
            [thumbnail] => Array
            (
                [file] => file_name.png
                [width] => 150
                [height] => 150
            )
    
            [medium] => Array
            (
                [file] => logo2-300x145.png
                [width] => 300
                [height] => 145
            )
        )
        [image_meta] => Array
        (
            [aperture] => 0
            [credit] => 
            [camera] => 
            [caption] => 
            [created_timestamp] => 0
            [copyright] => 
            [focal_length] => 0
            [iso] => 0
            [shutter_speed] => 0
            [title] => 
        )
    )
    

    If the image is attached to the post you can use get_children():

    $id = intval( $post->ID );  //The ID of the post attached to.
    $attachments = get_children( array(
                'post_parent' => $id,
                'post_status' => 'inherit',
                'post_type' => 'attachment',
                'post_mime_type' => 'image',
                'order' => 'ASC',
                'orderby' => 'menu_order'
            ) );
    
    foreach ( $attachment as $id => $attachment ) {
          $caption = $attachment['image_meta']->caption;
        }
    
  2. If you’re copying the article, these fields don’t exist. It would probably be easiest to add some form fields and get those values.

    If you’re assuming that these images are already uploaded to WordPress and stored in wp-content, you can find the images in the wp database in wp-posts under post-type attatchment (source).

    But again, the way the article has things set up, it looks like images can be uploaded from a user’s computer or another website, so it would probably be best just to ask them to fill in the fields.

  3. HERE YOU GO. If I understood correctly, you want to get info after attachment was uploaded.

    So put this code in functions.php file. And here you have 2 functions – one is fired after image is uploaded and displayed to edit info (or always whenever you edit attachment) and second one is fired when user clicks save on attachment.

    With those 2 functions you can make anything what you want to atchieve 🙂

    add_filter("attachment_fields_to_edit", "wpse_54409_image_details_on_edit", null, 2); 
    add_filter("attachment_fields_to_save", "wpse_54409_image_details_on_save", null , 2);
    
    function wpse_54409_image_details_on_edit($form_fields, $post) {
         //we interested only in images so ->
         if( substr($post->post_mime_type, 0, 5) == 'image' ){
             $meta = get_post_custom($post->ID);
              echo 'This attachment ID: '.$post->ID;
              echo '<br>';
              echo 'This attachment title: '.$post->post_title;
              echo '<br>';
              echo 'This attachment post name: '.$post->post_name; 
              echo '<br>'; 
              echo 'Alt. text: '.$meta['_wp_attachment_image_alt'][0]; 
              echo '<br>'; 
              echo 'File name: '.$meta['_wp_attached_file'][0]; 
         }
         return $form_fields;
     }
    
    function wpse_54409_image_details_on_save($post, $attachment) {
        //we interested only in images so ->
        if( substr($post->post_mime_type, 0, 5) == 'image' ){
            /*
            HERE YOU CAN GET ALL INFO UPPON ATTACHMENT IS SAVED 
            AND DO WITH THAT INFO WHATEVER YOU WANT
            EVEN ALTER IT TO MAKE CHANGES TO IT
            */
        }
        return $post;
    }