get_attached_file()
Retrieve attached file path based on attachment ID.
By default the path will go through the get_attached_file but passing a true to the $unfiltered argument of get_attached_file() will return the file path unfiltered.
The function works by getting the single post meta name, named '_wp_attached_file' and returning it. This is a convenience function to prevent looking up the meta name and provide a mechanism for sending the attached filename through a filter.
Uses: get_post_meta()
1 time — 0.000594 sec (slow) | 50000 times — 0.41 sec (very fast) | PHP 7.0.8, WP 4.6
Hooks from the function
Return
String|false
. The file path to where the attached file should be, false otherwise.
Usage
get_attached_file( $attachment_id, $unfiltered );
- $attachment_id(int) (required)
- Attachment ID.
- $unfiltered(true|false)
- Whether to apply filters.
Default: false
Examples
#1 Get the full path to the attachment file
$attach_id = 5; $attached_path = get_attached_file( $attach_id ); //> /home/www/example.com/wp-content/uploads/2014/11/file_name.png $filename = basename( $attached_path ); //> file_name.png
Changelog
Since 2.0.0 | Introduced. |