5

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?

8

PHP has a function for this purpose: exif_read_data
I used this image for testing.
Try this code for your purpose:

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>'
        );

        // Take image date 
        if ( function_exists( 'exif_read_data' ) ) {
            $exif = exif_read_data( $image->guid );
            if ( ! empty( $exif['DateTime'] ) ) {
                //var_dump( $exif['DateTime'] );
                $post['post_date'] = $exif['DateTime'];
            }
        }

        $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
            ) );
        }
    }
}
  • Thanks for the reply! Gave it a shot and got this error "Warning: checkdate() expects parameter 1 to be long, string given in /.../wp-includes/functions.php on line 4710 4403". No post was created. – Arkuen Oct 13 '15 at 15:29
  • Addt'l: I tested via this website to check if the metadata was correct and the date/time were indeed properly embedded into the image. Strangely, the sample image you tested with went through without any errors. – Arkuen Oct 13 '15 at 15:45
  • Got it! Did a var_dump and turns out DateTime output string(11) "1 (Windows)". Not sure why but probably because the image went through some editing software first. Easy fix by changing DateTime to DateTimeOriginal. Thanks a ton! – Arkuen Oct 13 '15 at 15:58
  • Make sure where you deploy your theme that you check if the EXIF extension is available/enabled. – Gabb3 Oct 14 '15 at 6:31

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.