2

Whenever I post a post with a featured image, I want to extract the exif data from that image and attach it as a term for a taxonomy. I already got a plugin which can extract the relevant info, but I dont know where and when to call wp_set_object_terms

Can anyone point me in the right direction?

1

Retrieving attachment image EXIF data

As "a plugin" won't help later visitors, let's start off with how to retrieve that:

# Get the post ID: Use one of the following. Preferred is the API function get_the_ID()
# $GLOBALS['post']->ID;
# global $post; $post->ID;
# get_the_ID();
$meta = wp_get_attachment_metadata( get_the_ID(), false );
# Fetch the EXIF data part 
$exif = $meta['image_meta'];

Now we have the EXIF data saved to $exif - to make sure we are not getting hacked with an image, don't forget to escape/sanitize the data before displaying (or better: saving)t it.

# List of EXIF data entries:
$created   = date_i18n( 
    get_option( 'date_format' ), 
    strtotime( $meta['created_timestamp'] ) 
);
$copyright = filter_var( $meta['copyright'] );
$credit    = filter_var( $meta['credit'] );
$title     = filter_var( $meta['title'] );
$camera    = filter_var( $meta['camera'] );
$shutter   = number_format_i18n( filter_var( 
    $meta['shutter_speed'],
    FILTER_SANITIZE_NUMBER_FLOAT
), 2 );
$iso       = number_format_i18n( filter_var( 
    $meta['iso'], 
    FILTER_SANITIZE_NUMBER_FLOAT 
) );
$focal     = number_format_i18n( filter_var( 
    $meta['focal_length'],
    FILTER_SANITIZE_NUMBER_FLOAT
), 2 );
$aperture  = filter_var( $meta['aperture'] );

Saving image EXIF meta as term

This possibly (or in nearly every case) will happen in the admin, we will need to use the hooks present there:

  • add_attachment (arg: $att_id)
  • edit_attachment (arg: $att_id)

where both are triggered after (what else) an image was uploaded or edited.

Then there is

  • media_send_to_editor (args: $html, $send_id, $attachment)
  • attachment_fields_to_save (args: $post, $attachment)

(more info in this answer)

So the only thing to do is to

  1. chose the right hook for your case
  2. extract the EXIF data that you want to add as term
  3. add the term using wp_set_object_terms() to your desired taxonomy and post in a callback attached to the filter or hook you have chosen above:

    $terms = wp_set_object_terms( 
        get_the_ID(),
        array( $meta['copyright'] ),
        'your-taxonomy',
        # Do *append* the new term and not replace all others with it!
        TRUE
    );
    # DEBUG:
    if ( is_wp_error( $terms ) )
        exit( $terms->get_error_message() );
    
    var_dump( $terms ); // should be an array of terms
    

You should always check the return value (like I did with is_wp_error()) when processing something. If you want to be nice and this is something where errors can happen due to missing EXIF data, then you should also jump in there and provide some user feedback - an admin notice for example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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