Last active
December 17, 2017 07:58
-
-
Save blainerobison/fecf1bba78527712fd6e to your computer and use it in GitHub Desktop.
wp: Add Attachment Metadata [WordPress]
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
/** | |
* 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