WordPress memorizza anche i metadati associati a ciascuna immagine caricata nella Media Library. Se stiamo realizzando un sito orientato alle foto caricate dagli utenti possiamo sfruttare tale caratteristica a nostro vantaggio.
Definiamo la seguente funzione che accetta come argomento l'ID dell'immagine così come è stato definito da WordPress:
function my_get_image_data( $image_id ) {
$data = wp_get_attachment_metadata( $image_id );
$meta = $data['image_meta'];
$meta_data = array();
$aperture = $meta['aperture'];
$camera = $meta['camera'];
$created = strftime( '%d-%m-%Y', $meta['created_timestamp'] );
$focal_length = $meta['focal_length'];
$shutter_speed = $meta['shutter_speed'];
$meta_data['aperture'] = $aperture;
$meta_data['camera'] = $camera;
$meta_data['created'] = $created;
$meta_data['focal-length'] = $focal_length;
$meta_data['shutter-speed'] = $shutter_speed;
return $meta_data;
}
La funzione restituisce un array associativo con le informazioni richieste. Esempio di uso nel Loop:
$post_id = get_the_ID();
$thumb_id = get_post_thumbnail_id( $post_id );
$image_meta = my_get_image_data( $thumb_id );