Answer the question
In order to leave comments, you need to log in
How to store received EXIF image metadata in WPdb?
Based on the built-in WP function , wp_read_image_metadata
I created a custom wp_read_image_metadata_exif
one that gets the EXIF image fields I need. Next, I concocted a function that displays the values of these fields in the media editor:
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 );
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
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 );
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 questionAsk a Question
731 491 924 answers to any question