Last active
December 19, 2015 13:49
-
-
Save attitude/5964722 to your computer and use it in GitHub Desktop.
Returns metadata for current WordPress attachment or array of attachments
This file contains 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
<?php | |
/** | |
* Returns metadata for current WordPress attachment or array of attachments | |
* | |
* Reads all attachment metadata and expands it. URLs are rewritten with upload | |
* dir prepended, file size is calculated if function exists. By default url protocol | |
* is removed, use `add_filter()` to disable. | |
* | |
* @uses {@link apply_filters() http://codex.wordpress.org/Function_Reference/apply_filters} | |
* @uses {@link file_size_format() https://gist.github.com/attitude/5964642} | |
* | |
* @param int|array $id Array of IDs or a single id | |
* @returns array Attachment metadata | |
* | |
*/ | |
function wp_get_attachment_meta($id) | |
{ | |
if (is_array($id)) { | |
$files = array(); | |
// Apply intval() on each array item | |
$id = array_map('intval', $id); | |
// Prefetch data, should minimize queries needed | |
update_meta_cache('post', $id); | |
foreach ($id as $the_id) { | |
if ((int) $the_id > 0) { | |
$files[] = wp_get_attachment_meta($the_id); | |
} | |
} | |
return $files; | |
} | |
static $uploadpath = null; | |
static $uploadurl = null; | |
if (!$uploadpath) { | |
$upload_path = wp_upload_dir(); | |
if (!$upload_path || !isset($upload_path['path'])) { | |
exit('Cannot continue. Upload path is not set correctly'); | |
} | |
$uploadurl = !! apply_filters('wp_get_attachment_meta/remove_protocol', true) ? strtr($upload_path['baseurl'], array('http:' => '', 'https:' => '')) : $upload_path['baseurl']; | |
$uploadpath = $upload_path['basedir']; | |
unset($upload_path); | |
} | |
$metadata = get_post_meta($id); | |
foreach ($metadata as $metakey => &$meta) { | |
$new_metadata[str_replace('_wp_attached_', '', str_replace('_wp_attachment_', '', $metakey))] = maybe_unserialize($meta[0]); | |
} | |
if (isset($new_metadata['metadata']) && isset($new_metadata['metadata']['sizes']) && class_exists('BetterWPMedia')) { | |
$new_metadata['metadata'] = BetterWPMedia::generate_and_clean($new_metadata['metadata'], $id); | |
} | |
if (isset($new_metadata['file']) && (!isset($new_metadata['file_size']) || (function_exists('file_size_format') && !isset($new_metadata['file_size_formatted'])))) { | |
$path_to_file = realpath($uploadpath.'/'.$new_metadata['file']); | |
if ($path_to_file) { | |
$file_size = filesize($path_to_file); | |
$new_metadata['file_size'] = $file_size; | |
update_post_meta($id, '_wp_attached_file_size', $new_metadata['file_size']); | |
if (function_exists('file_size_format')) { | |
$new_metadata['file_size_formatted'] = file_size_format($file_size); | |
update_post_meta($id, '_wp_attached_file_size_formatted', $new_metadata['file_size_formatted']); | |
} | |
} | |
} | |
if (isset($new_metadata['file'])) { | |
$new_metadata['metadata']['file'] = $uploadurl.'/'.$new_metadata['file']; | |
$new_metadata['guid'] =& $new_metadata['metadata']['file']; | |
$new_metadata['file'] =& $new_metadata['metadata']['file']; | |
} | |
if (isset($new_metadata['metadata']) && isset($new_metadata['metadata']['sizes'])) { | |
global $_wp_additional_image_sizes; | |
foreach ($new_metadata['metadata']['sizes'] as $size => &$sizedata) { | |
$sizedata['file'] = dirname($new_metadata['file']).'/'.$sizedata['file']; | |
$sizedata['img'] = wp_get_attachment_image($id, $size); | |
} | |
} | |
$new_metadata['id'] = $id; | |
return $new_metadata; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment