• Hello,

    I am created a SIRI shortcut to upload photos from Lightroom CC on my iPad via REST API (the provided API from wordpress) to the media library of WordPress. This works fine. But now I would like to see that the post meta (title, alt, caption, description) are filled with the EXIF/ITPC data of the photo.

    I don’t want to use a plugin and read something in the documentation and learned that I can get all stored metadata from the function wp_read_image_metadata( $attachment_path ).

    So I’ve created a function and add an add_action call:

    
    add_action( 'add_attachment', 'my_set_image_meta_upon_image_upload' );
    function my_set_image_meta_upon_image_upload( $post_ID ) {
      if ( wp_attachment_is_image( $post_ID ) ) {
        $attachment_path = get_attached_file( $post->ID );
        $metadata = wp_read_image_metadata( $attachment_path );
        $my_image_title = $metadata["title"]."-title";
        $my_image_meta = array(
          'ID' => $post_ID,	
          'post_title' => $my_image_title,	
          'post_excerpt' => $my_image_title,
          'post_content'	=> $my_image_title
        );
        update_post_meta( $post_ID, '_wp_attachment_image_alt', $my_image_title );
        wp_update_post( $my_image_meta );
      } 
    }
    

    But the metadata object seems to be empty. My suspicion is that the action isn’t the right one and that at this time the metadata not written. But what is the right way to do this? What is the right hook or action to do this?

    Thanks in advance
    Thorsten

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter thorque

    (@thorque)

    The relevant entry out of the database is this one:

    
    a:5:{s:5:"width";i:2400;s:6:"height";i:1350;s:4:"file";s:20:"2020/05/IMG_0300.jpg";s:5:"sizes";a:13:{s:6:"medium";a:4:{s:4:"file";s:20:"IMG_0300-640x360.jpg";s:5:"width";i:640;s:6:"height";i:360;s:9:"mime-type";s:10:"image/jpeg";}s:5:"large";a:4:{s:4:"file";s:21:"IMG_0300-1280x720.jpg";s:5:"width";i:1280;s:6:"height";i:720;s:9:"mime-type";s:10:"image/jpeg";}s:9:"thumbnail";a:4:{s:4:"file";s:20:"IMG_0300-150x150.jpg";s:5:"width";i:150;s:6:"height";i:150;s:9:"mime-type";s:10:"image/jpeg";}s:12:"medium_large";a:4:{s:4:"file";s:20:"IMG_0300-768x432.jpg";s:5:"width";i:768;s:6:"height";i:432;s:9:"mime-type";s:10:"image/jpeg";}s:9:"1536x1536";a:4:{s:4:"file";s:21:"IMG_0300-1536x864.jpg";s:5:"width";i:1536;s:6:"height";i:864;s:9:"mime-type";s:10:"image/jpeg";}s:9:"2048x2048";a:4:{s:4:"file";s:22:"IMG_0300-2048x1152.jpg";s:5:"width";i:2048;s:6:"height";i:1152;s:9:"mime-type";s:10:"image/jpeg";}s:6:"awb_sm";a:4:{s:4:"file";s:20:"IMG_0300-500x281.jpg";s:5:"width";i:500;s:6:"height";i:281;s:9:"mime-type";s:10:"image/jpeg";}s:6:"awb_md";a:4:{s:4:"file";s:20:"IMG_0300-800x450.jpg";s:5:"width";i:800;s:6:"height";i:450;s:9:"mime-type";s:10:"image/jpeg";}s:6:"awb_lg";a:4:{s:4:"file";s:21:"IMG_0300-1280x720.jpg";s:5:"width";i:1280;s:6:"height";i:720;s:9:"mime-type";s:10:"image/jpeg";}s:6:"awb_xl";a:4:{s:4:"file";s:22:"IMG_0300-1920x1080.jpg";s:5:"width";i:1920;s:6:"height";i:1080;s:9:"mime-type";s:10:"image/jpeg";}s:28:"ab-block-post-grid-landscape";a:4:{s:4:"file";s:20:"IMG_0300-600x400.jpg";s:5:"width";i:600;s:6:"height";i:400;s:9:"mime-type";s:10:"image/jpeg";}s:25:"ab-block-post-grid-square";a:4:{s:4:"file";s:20:"IMG_0300-600x600.jpg";s:5:"width";i:600;s:6:"height";i:600;s:9:"mime-type";s:10:"image/jpeg";}s:13:"crp_thumbnail";a:4:{s:4:"file";s:20:"IMG_0300-230x129.jpg";s:5:"width";i:230;s:6:"height";i:129;s:9:"mime-type";s:10:"image/jpeg";}}s:10:"image_meta";a:12:{s:8:"aperture";s:1:"9";s:6:"credit";s:18:"PICTOR PHOTOGRAPHY";s:6:"camera";s:9:"ILCE-6500";s:7:"caption";s:148:"Es gibt für das Smart Keyboard Folio auch eine etwas steilere Aufstellposition. Diese betr?gt etwa 65° anstatt den 50° in der normalen Position.";s:17:"created_timestamp";s:10:"1581119063";s:9:"copyright";s:25:"(c) by PICTOR PHOTOGRAPHY";s:12:"focal_length";s:2:"28";s:3:"iso";s:3:"100";s:13:"shutter_speed";s:3:"0.5";s:5:"title";s:20:"Smart Keyboard Folio";s:11:"orientation";s:1:"0";s:8:"keywords";a:0:{}}}
    
    Moderator bcworkz

    (@bcworkz)

    You might try the ‘rest_insert_attachment’ action instead. However, the API already calls wp_read_image_metadata() itself, so I don’t think a different action will help. The function calls PHP’s iptcparse() to extract data from the image file, but only does so if getimagesize() returns extended information which includes IPTC data. If the file format deviates from the expected in any way, the process could fail.

    If you know of a plugin that properly extracts the data, review how the plugin does this in its source code and use the same technique for yourself from the ‘rest_insert_attachment’ action.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Update post meta data with IPTC during upload of media’ is closed to new replies.