@lakegull

Как сохранить полученные EXIF метаданные изображения в WPdb?

На основе встроенной WP функции wp_read_image_metadata создал кастомную wp_read_image_metadata_exif, которая получает нужные мне EXIF поля изображения. Далее состряпал функцию, которая выводит значения этих полей в редакторе медиафайлов:
61a4fb9154615220610661.png
Код:
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 );


Подскажите, как сохранить эти значения в базе данных WP, в wp_postmeta?

Пробовал следующее, не работает:
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 );
  • Вопрос задан
  • 62 просмотра
Решения вопроса 1
wppanda5
@wppanda5 Куратор тега WordPress
WordPress Mедведь
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 );
Ответ написан
Пригласить эксперта
Ответы на вопрос 1
qant
@qant
programer
Вроде все правильно, и update_post_meta( $post['ID'], 'locationname', esc_url( $attachment['locationname'] ) ); должно работать.

Проверь все ли параметры функции ок, возможно где то чего то не хватает, или вместо строки приходит массив или пусто там вовсе и тп

Если нужно массив сохранять то сериализовать нужно
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы