L
L
lakegull2021-11-29 19:18:09
WordPress
lakegull, 2021-11-29 19:18:09

How to store received EXIF ​​image metadata in WPdb?

Based on the built-in WP function , wp_read_image_metadataI created a custom wp_read_image_metadata_exifone that gets the EXIF ​​image fields I need. Next, I concocted a function that displays the values ​​of these fields in the media editor:
61a4fb9154615220610661.png
Code:

function wp_read_image_metadata_exif( $file ) {
  if ( ! file_exists( $file ) ) {
    return false;
  }

  list( , , $image_type ) = wp_getimagesize( $file );

  if ( is_callable( 'iptcparse' ) ) {
    wp_getimagesize( $file, $info );

    if ( ! empty( $info['APP13'] ) ) {

      if ( defined( 'WP_DEBUG' ) && WP_DEBUG
        && ! defined( 'WP_RUN_CORE_TESTS' )
      ) {
        $iptc = iptcparse( $info['APP13'] );
      } else {
        $iptc = @iptcparse( $info['APP13'] );
      }
      
      if ( ! empty( $iptc['2#090'][0] ) ) { // City.
        $meta['city'] = trim( $iptc['2#090'][0] );
      }
      if ( ! empty( $iptc['2#027'][0] ) ) { // Location Name.
        $meta['locationname'] = trim( $iptc['2#027'][0] );
      }
    }
  }

  return apply_filters( 'wp_read_image_metadata_exif', $meta, $file, $iptc );

}

function display_exif_fields ( $form_fields, $post ){

$type = get_post_mime_type( $post->ID );

$attachment_path = get_attached_file( $post->ID );

$metadata = wp_read_image_metadata_exif( $attachment_path );

  $form_fields['city'] = array(
    'label' => 'City',
    'input' => 'text',
    'value' => $metadata['city'],
    'helps' => '',
  );
  
  $form_fields['locationname'] = array(
    'label' => 'Location name',
    'input' => 'text',
    'value' => $metadata['locationname'],
    'helps' => '',
  );

    return $form_fields;
}

add_filter( 'attachment_fields_to_edit', 'display_exif_fields', 10, 2 );


Can you tell me how to store these values ​​in the WP database, in wp_postmeta?

Tried the following, doesn't work:
function save_exif_fields( $post, $attachment ) {
  if( isset( $attachment['city'] ) )
    update_post_meta( $post['ID'], 'city', $attachment['city'] );

  if( isset( $attachment['locationname'] ) )
update_post_meta( $post['ID'], 'locationname', esc_url( $attachment['locationname'] ) );

  return $post;
}

add_filter( 'attachment_fields_to_save', 'save_exif_fields', 10, 2 );

Answer the question

In order to leave comments, you need to log in

2 answer(s)
W
WP Panda, 2021-11-30
@lakegull

function display_exif_fields ( $form_fields, $post ){

$type = get_post_mime_type( $post->ID );

$attachment_path = get_attached_file( $post->ID );

$metadata = wp_read_image_metadata_exif( $attachment_path );

$city = get_post_meta( $post->ID, 'city', true );
$locationname = get_post_meta( $post->ID, 'locationname', true );

  $form_fields['city'] = array(
    'label' => 'City',
    'input' => 'text',
    'value' => !empty($city) ? $city : $metadata['city'],
    'helps' => '',
  );
  
  $form_fields['locationname'] = array(
    'label' => 'Location name',
    'input' => 'text',
    'value' => !empty($locationname) ? $locationname : $metadata['locationname'],
    'helps' => '',
  );

    return $form_fields;
}

add_filter( 'attachment_fields_to_edit', 'display_exif_fields', 10, 2 );


function save_exif_fields( $post, $attachment ) {

    $array = [ 'city', 'locationname' ];
    foreach ( $array as $one ) {
      if ( ! empty( $attachment[ $one ] ) ) {
        update_post_meta( $post[ 'ID' ], $one, $attachment[ $one ] );
      } else {
        delete_post_meta( $post[ 'ID' ], $one );
      }
    }

    return $post;
  }

  add_filter( 'attachment_fields_to_save', 'save_exif_fields', 10, 2 );

A
Anton, 2021-11-30
Semenov

Everything seems to be correct, and update_post_meta( $post['ID'], 'locationname', esc_url( $attachment['locationname'] ) ); should work.
Check if all the parameters of the function are ok, perhaps something is missing somewhere, or instead of a string an array comes or it is empty there at all, etc.
If you need to save the array, then you need to serialize

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question