Skip to content

Instantly share code, notes, and snippets.

@blainerobison
Last active December 17, 2017 07:58
Show Gist options
  • Save blainerobison/fecf1bba78527712fd6e to your computer and use it in GitHub Desktop.
Save blainerobison/fecf1bba78527712fd6e to your computer and use it in GitHub Desktop.
wp: Add Attachment Metadata [WordPress]
/**
* Add attachment metadata
*
* By default, WordPress only adds '_wp_attachment_metadata' metadata to images, audio and video files.
* This hooks into 'wp_update_attachment_metadata' and sets 'file' to the value of '_wp_attached_file'.
*/
function prefix_update_attachment_metadata( $data, $post_id ) {
// only set 'file' if needed
if ( isset( $data['file'] ) ) {
return $data;
}
// get file path e.g. pdf/filename.pdf
if ( $file = get_post_meta( $post_id, '_wp_attached_file', true ) ) {
$data['file'] = $file;
}
return $data;
}
add_filter( 'wp_update_attachment_metadata', 'prefix_update_attachment_metadata', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment