Created
January 16, 2015 00:11
-
-
Save owldesign/eb2bfe7569a51a61808d to your computer and use it in GitHub Desktop.
ACF image field to display attachment_metadata for EXIF
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* format_value() | |
* | |
* This filter is appied to the $value after it is loaded from the db and before it is returned to the template | |
* | |
* @type filter | |
* @since 3.6 | |
* @date 23/01/13 | |
* | |
* @param $value (mixed) the value which was loaded from the database | |
* @param $post_id (mixed) the $post_id from which the value was loaded | |
* @param $field (array) the field array holding all the field options | |
* | |
* @return $value (mixed) the modified value | |
*/ | |
function format_value( $value, $post_id, $field ) { | |
// bail early if no value | |
if( empty($value) ) { | |
return $value; | |
} | |
// convert to int | |
$value = intval($value); | |
// format | |
if( $field['return_format'] == 'url' ) { | |
$value = wp_get_attachment_url( $value ); | |
} elseif( $field['return_format'] == 'array' ) { | |
$attachment = get_post( $value ); | |
// validate | |
if( !$attachment ) { | |
return false; | |
} | |
// create array to hold value data | |
$src = wp_get_attachment_image_src( $attachment->ID, 'full' ); | |
$value = array( | |
'ID' => $attachment->ID, | |
'id' => $attachment->ID, | |
'alt' => get_post_meta($attachment->ID, '_wp_attachment_image_alt', true), | |
'title' => $attachment->post_title, | |
'caption' => $attachment->post_excerpt, | |
'description' => $attachment->post_content, | |
'url' => $src[0], | |
'width' => $src[1], | |
'height' => $src[2], | |
); | |
// find all image meta | |
$image_meta = wp_get_attachment_metadata($attachment->ID); | |
if( $image_meta ) { | |
$value['image_meta'] = array(); | |
$value['image_meta']['aperture' ] = $image_meta[image_meta][aperture]; | |
$value['image_meta']['credit' ] = $image_meta[image_meta][credit]; | |
$value['image_meta']['camera' ] = $image_meta[image_meta][camera]; | |
$value['image_meta']['caption' ] = $image_meta[image_meta][caption]; | |
$value['image_meta']['created_timestamp' ] = $image_meta[image_meta][created_timestamp]; | |
$value['image_meta']['copyright' ] = $image_meta[image_meta][copyright]; | |
$value['image_meta']['focal_length' ] = $image_meta[image_meta][focal_length]; | |
$value['image_meta']['iso' ] = $image_meta[image_meta][iso]; | |
$value['image_meta']['shutter_speed' ] = $image_meta[image_meta][shutter_speed]; | |
$value['image_meta']['title' ] = $image_meta[image_meta][title]; | |
$value['image_meta']['orientation' ] = $image_meta[image_meta][orientation]; | |
} | |
// find all image sizes | |
$image_sizes = get_intermediate_image_sizes(); | |
if( $image_sizes ) { | |
$value['sizes'] = array(); | |
foreach( $image_sizes as $image_size ) { | |
// find src | |
$src = wp_get_attachment_image_src( $attachment->ID, $image_size ); | |
// add src | |
$value['sizes'][ $image_size ] = $src[0]; | |
$value['sizes'][ $image_size . '-width' ] = $src[1]; | |
$value['sizes'][ $image_size . '-height' ] = $src[2]; | |
} | |
// foreach( $image_sizes as $image_size ) | |
} | |
// if( $image_sizes ) | |
} | |
return $value; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment