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.

After you have uploaded an attachment in Wordpress using the media upload screen, is there a hook or filter I can run after the image has been uploaded where I can get the path to the uploaded image so I can analyse it?

I am building a plugin that will analyse an image after it has been uploaded and then tag the image with the average colour it finds in the image. The only problem is I don't know what hook I can use that will fire right after the image has uploaded and then a way I can get the path to the newly uploaded file.

Any help would be gratefully appreciated.

share|improve this question
add comment

3 Answers

up vote 6 down vote accepted

Turns out I solved my own question with help from a colleague. The two filters that get called after media is uploaded or when media is being edited are; 'add_attachment' and 'edit_attachment'. Here is the code I am using, I then check to see if the attachment is an image (code omitted from example).

function analyse_attachment($attachment_ID)
{          
    $attachment = get_attached_file($attachment_ID); // Gets path to attachment
    update_post_meta($attachment_ID, "image_rgb", $the_colour);
}

add_action("add_attachment", 'analyse_attachment');
add_action("edit_attachment", 'analyse_attachment');

Obviously I've omitted some stuff that isn't relevant to the question. But that code gets called right after you've uploaded or edited an attachment.

share|improve this answer
add comment

you have two filters you can use: attachment_fields_to_save which gets two paramaters ( $post, $attachment) so :

add_filter('attachment_fields_to_save',your_image_analyse_function);

function your_image_analyse_function($post, $attachment){
  $attachment['url']
  //do your stuff
}

and media_send_to_editor which gets 3 paramaters ( $html, $send_id, $attachment) and fires after you click the send to editor so once again you can use $attachment.

add_filter('media_send_to_editor',your_image_analyse_function);

function your_image_analyse_function($html, $send_id, $attachment){
  $attachment['url']
  //do your stuff
}
share|improve this answer
    
Thanks Bainternet, very useful. Although, neither of those were what I was looking for. After asking our senior PHP developer at work, he browsed through the Wordpress core files and found an action that gets called right after an image or whatever else is uploaded. Well, there's two of them: 'add_attachment' and 'edit_attachment' I'll post it as an answer for other people. –  DigitalSea Mar 18 '11 at 10:24
add comment

HTML Markup:

<p>
  <label for="custom-upload">Upload New Image:</label>
  <input type="file" tabindex="3" name="custom-upload" id="custom-upload" />
</p>
<?php
  /*Retrieving the image*/
  $attachment = get_post_meta($postid, 'custom_image');

  if($attachment[0]!='')
  {
   echo wp_get_attachment_link($attachment[0], 'thumbnail', false, false);
  }

?>

Uploading the image:

<?php
global $post; /*Global post object*/
$post_id = $post->ID; /*Geting current post id*/
$upload = $_FILES['upload']; /*Receive the uploaded image from form*/
add_custom_image($post_id, $upload); /*Call image uploader function*/

function add_custom_image($post_id, $upload)
{
 $uploads = wp_upload_dir(); /*Get path of upload dir of wordpress*/

 if (is_writable($uploads['path']))  /*Check if upload dir is writable*/
 {
  if ((!empty($upload['tmp_name'])))  /*Check if uploaded image is not empty*/
  {
   if ($upload['tmp_name'])   /*Check if image has been uploaded in temp directory*/
   {
    $file=handle_image_upload($upload); /*Call our custom function to ACTUALLY upload the image*/

    $attachment = array  /*Create attachment for our post*/
    (
      'post_mime_type' => $file['type'],  /*Type of attachment*/
      'post_parent' => $post_id,  /*Post id*/
    );

    $aid = wp_insert_attachment($attachment, $file['file'], $post_id);  /*Insert post attachment and return the attachment id*/
    $a = wp_generate_attachment_metadata($aid, $file['file'] );  /*Generate metadata for new attacment*/
    $prev_img = get_post_meta($post_id, 'custom_image');  /*Get previously uploaded image*/
    if(is_array($prev_img))
    {
     if($prev_img[0] != '')  /*If image exists*/
     {
      wp_delete_attachment($prev_img[0]);  /*Delete previous image*/
     }
    }
    update_post_meta($post_id, 'custom_image', $aid);  /*Save the attachment id in meta data*/

    if ( !is_wp_error($aid) ) 
    {
     wp_update_attachment_metadata($aid, wp_generate_attachment_metadata($aid, $file['file'] ) );  /*If there is no error, update the metadata of the newly uploaded image*/
    }
   }
  }
  else
  {
   echo 'Please upload the image.';
  }
 }
}

function handle_image_upload($upload)
{
 global $post;

        if (file_is_displayable_image( $upload['tmp_name'] )) /*Check if image*/
        {
            /*handle the uploaded file*/
            $overrides = array('test_form' => false);
            $file=wp_handle_upload($upload, $overrides);
        }
 return $file;
}
?>
share|improve this answer
add comment

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.