I currently have the following working function.
add_action('add_attachment', 'create_post_from_image');
function create_post_from_image($id) {
if (wp_attachment_is_image($id)) {
$image = get_post($id);
// get image height/width for auto inserting the image later
@list($width, $height) = getimagesize(get_attached_file($id));
$post = array(
// Set image title as post title
'post_title' => $image->post_title,
// Set post to draft for details
'post_status' => 'draft',
// "Fake" WordPress insert image code
'post_content' => '<a href="'.$image->guid.'"><img class="alignnone size-full wp-image-'.$image->ID.'" src="'.$image->guid.'" alt="'.$image->post_name.'" width="'.$width.'" height="'.$height.'" /></a>'
);
$postid = wp_insert_post($post);
if ($postid) {
// Set image as post featured image
set_post_thumbnail($postid, $image->ID);
// Attach image to the post
wp_update_post(array(
'ID' => $id,
'post_parent' => $postid
)
);
}
}
}
What this function does, essentially, is create posts for each image uploaded to Media Library and embed the uploaded image into the post content. It then sets it as a draft for me to review and publish.
How do I modify it so that for each image uploaded, it will get the embedded EXIF data and take the date/time the image was captured, then automatically set that as the date/time of the WP post that is created?