Skip to content

Instantly share code, notes, and snippets.

@DrewAPicture
Last active December 22, 2015 02:49
Show Gist options
  • Save DrewAPicture/6406147 to your computer and use it in GitHub Desktop.
Save DrewAPicture/6406147 to your computer and use it in GitHub Desktop.
Use attachment date from EXIF data on image upload.
<?php
class Unnamed_Theme {
public function __construct() {
add_filter( 'wp_update_attachment_metadata', array( $this, 'update_image_date' ), 10, 2 );
}
/**
* Update the 'post_date' field with the date in the image meta
*
* @param array $data The attachment meta array.
* @param int $post_id The attachment ID.
*
* @return array The image meta data array.
*/
function update_image_date( $data, $post_id ) {
global $wpdb;
if ( isset( $data['image_meta']['created_timestamp'] ) ) {
// Save the original date
$original = get_post_field( 'post_date', $post_id );
update_post_meta( $post_id, '_original_upload', $original );
// Update with the date from EXIF
$wpdb->update( $wpdb->posts, array( 'post_date' => gmdate( 'Y-m-d H:i:s', $data['image_meta']['created_timestamp'] ) ), array( 'ID' => $post_id ) );
}
return $data;
}
} // Unnamed Theme
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment