Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

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.
share|improve this question
    
please specify more. When you and where you want to get that data? After images was uploaded, or sometime else? If not after upload-then attachments are the same as regular posts so you can use the same techniq as for posts. I have expiriance in attachments, but i need more info to help you. –  Tommixoft Jun 6 '12 at 21:21
    
@Tommixoft , thank you. Just update my answer. –  Eray Jun 6 '12 at 21:52

3 Answers 3

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.

share|improve this answer

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;
    }
share|improve this answer
    
if it's not attached to any post ? –  Eray Jun 6 '12 at 22:53
    
@Eray - 'post_parent' => 0,, and if this argument is not provided‌​, the default is 'no parent'. –  brasofilo Dec 8 '12 at 15:04

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;
}
share|improve this answer

Your Answer

 
discard

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.