8

I am trying to have WordPress automatically save the "credit" metadata that is stored within images I am uploading. I know this would be entered as a custom post meta field using update_post_meta().

The "credit" information is gathered by wp_read_image_metadata(), but media_handle_upload() doesn't use it. It only uses the "caption" and "title" information gathered from the image for post_content and post_title respectively.

I've found several examples of how to add custom attachment metadata fields after the upload when editing the image, but since WP is already gathering this information from the image's metadata, I'd like to find a way to have it saved as the image is uploaded.

I'm unclear of what steps to take to hook into media_handle_upload() (there doesn't seem to be a hook) so that I can call update_post_meta to save the "credit" line for the attachment.

2
  • 1
    This is a great question, I will try and answer it when I have time or add a bounty to it. I did try for 15 minutes and it produced some amazingly detailed errors like "an error occurred in the upload. Please try again later", even though it was working.
    – Wyck
    Commented Mar 16, 2013 at 22:04
  • 2
    Hey @Wyck, indeed a great Q, I guess I was working on it at the same time ;)
    – brasofilo
    Commented Mar 16, 2013 at 22:38

1 Answer 1

7

You can use wp_generate_attachment_metadata:

This function generates metadata for an image attachment. It also creates a thumbnail and other intermediate sizes of the image attachment based on the sizes defined on the Settings_Media_Screen.

The second argument of the filter is the attachment ID, so it shouldn't be a problem to add the post meta:

add_filter( 'wp_generate_attachment_metadata', 'manipulate_metadata_wpse_91177', 10, 2 );

function manipulate_metadata_wpse_91177( $metadata, $attachment_id ) 
{
    // var_dump( $metadata['image_meta'] );
    // Credit is inside $metadata['image_meta']['credit']
    return $metadata;
}

Inspecting the contents of $metadata['image_meta'] with FirePHP, these are the results:

aperture: 4
camera: "DMC-FZ100"
caption: ""
copyright: ""
created_timestamp: 1352136868
credit: ""
focal_length: "9.2"
iso: "100"
shutter_speed: "0.003125"
title: "Double Dip"

[Update]
Full test adding a custom column in Media Library screen (/wp-admin/upload.php) to display the post meta info:

add_filter( 'wp_generate_attachment_metadata', 'manipulate_metadata_wpse_91177', 10, 2 );
add_filter( 'manage_upload_columns', 'camera_info_column_wpse_91177' );
add_action( 'manage_media_custom_column', 'camera_info_display_wpse_91177', 10, 2 );

function manipulate_metadata_wpse_91177( $metadata, $attachment_id ) 
{
    update_post_meta( $attachment_id, 'photo_title', $metadata['image_meta']['title'] );
    update_post_meta( $attachment_id, 'photo_camera', $metadata['image_meta']['camera'] );
    return $metadata;
}

function camera_info_column_wpse_91177( $columns ) 
{
    $columns['cam_info'] = 'Camera Info';    
    return $columns;
}

function camera_info_display_wpse_91177( $column_name, $post_id ) 
{
    if( 'cam_info' != $column_name || !wp_attachment_is_image( $post_id ) )
        return;

    $title = get_post_meta( $post_id, 'photo_title', true );
    $camera = get_post_meta( $post_id, 'photo_camera', true );
    $echo_title = $title ? 'Title: ' . $title . '<br />' : '';
    $echo_camera = $camera ? 'Camera: ' . $camera : '';
    echo $echo_title . $echo_camera;
}

camera info

Somewhat related Q&A: Create new category upon save based on post information

4
  • I think I mis-understood the question thankfully you answered, I was trying to set the "credit" field in the media box section so it could be altered by the end user if they wanted to change it from what the exif said..phew..
    – Wyck
    Commented Mar 17, 2013 at 1:24
  • @brasofilo Thanks again. On a side note, why does $attachment_id have to be the second argument on the filter? It doesn't work if it's the first argument. I wasn't able to find clarification in the add_filter docs.
    – edeneye
    Commented Mar 17, 2013 at 1:35
  • @user955664, that's The Way of the Hook or Follow the Source, Luke!
    – brasofilo
    Commented Mar 17, 2013 at 1:51
  • @brasofilo Thanks, again. It makes sense when looking at the apply_filters call to wp_generate_attachment_metadata. I had been looking at the function itself, not the apply_filters call.
    – edeneye
    Commented Mar 17, 2013 at 2:15

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.