get_attached_file( int $attachment_id, bool $unfiltered = false ): string|false
- Since
- 2.0.0
- Source
wp-includes/post.php:846
Description
By default the path will go through the 'get_attached_file' filter, but passing true to the $unfiltered argument will return the file path unfiltered.
The function works by retrieving the _wp_attached_file post meta value.
This is a convenience function to prevent looking up the meta name and provide a mechanism for sending the attached filename through a filter.
Compatibility
- WordPress
- since 2.0.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$attachment_idint- Attachment ID.
$unfilteredbooloptional- Whether to skip the 'get_attached_file' filter.
Default false.Default:false
Return value
string|false- The file path to where the attached file should be, false otherwise.
Performance profile
How much work a call to get_attached_file() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Trivial
- Scaling
- Constant
- Instructions
- 11–30
- Plugin surface
- 1 hook
- Called by
- 25
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 31.
Third-party callbacks on 'get_attached_file' run inside this call, and their cost is not bounded by anything here.
25 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly
Further down the call graph this can also reach cache, serialize, option, query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 4 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost get_attached_file() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 11–15 | get_post_meta() |
| always | 16–20 | get_post_meta(), apply_filters() |
!$file | 21–25 | get_post_meta(), wp_get_upload_dir() |
!$file | 26–30 | get_post_meta(), wp_get_upload_dir(), apply_filters() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 31 | 11–30 | 5 | |
| 8.5 | 31 | 11–30 | 5 | |
| 8.4 | 31 | 11–30 | 5 | 6 fewer instructions than PHP 8.3 |
| 8.3 | 37 | 11–36 | 5 | |
| 8.2 | 37 | 11–36 | 5 | |
| 8.1 | 37 | 11–36 | 5 | |
| 7.4 | 37 | 11–36 | 5 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Hooks and filters fired · 1
One hook fires while get_attached_file() runs, in this order:
- apply_filters( get_attached_file )filterline 869 (+23 into the body)
Filters the attached file based on the given ID.
Uses · 4
- get_post_meta()Retrieves a post meta field for the given post ID.
- str_starts_with()Polyfill for `str_starts_with()` function added in PHP 8.0.
- wp_get_upload_dir()Retrieves uploads directory information.
- apply_filters()Calls the callback functions that have been added to a filter hook.
Used by · 25
- Custom_Image_Header::step_2()Displays second step of custom header image page.
- Custom_Image_Header::step_3()Displays third step of custom header image page.
- File_Upload_Upgrader::__construct()Construct the upgrader for a form.
- WP_Customize_Manager::_validate_header_video()Callback for validating the header_video value.
- WP_Customize_Manager::import_theme_starter_content()Imports theme starter content into the customized state.
- WP_Media_List_Table::column_title()Handles the title column output.
- _copy_image_file()Copies an existing image file.
- _load_image_to_edit_path()Retrieves the path or URL of an attachment's attached file.
- attachment_submitbox_metadata()Displays non-editable attachment metadata in the publish meta box.
- get_attachment_icon_src()Retrieve icon URL and Path.
- get_attachment_taxonomies()Retrieves taxonomies attached to given the attachment.
- get_media_item()Retrieves HTML form for modifying the image attachment.
Show all 25
- image_downsize()Scales an image to fit a particular size (such as 'thumb' or 'medium').
- wp_attachment_is()Verifies an attachment is of a given type.
- wp_crop_image()Crops an image to a given size.
- wp_delete_attachment()Trashes or deletes an attachment.
- wp_get_attachment_link()Retrieves an attachment page link using an image or icon, if possible.
- wp_get_attachment_thumb_file()Retrieves thumbnail for an attachment.
- wp_get_original_image_path()Retrieves the path to an uploaded image file.
- wp_load_image()Load an image from a string, if PHP supports it.
- wp_maybe_generate_attachment_metadata()Maybe attempts to generate attachment metadata, if missing.
- wp_mime_type_icon()Retrieves the icon for a MIME type or attachment.
- wp_prepare_attachment_for_js()Prepares an attachment post object for JS, where it is expected to be JSON-encoded and fit into an Attachment model.
- wp_restore_image()Restores the metadata for a given attachment.
- wp_save_image()Saves image to post, along with enqueued changes in `$_REQUEST['history']`.
Source code
function get_attached_file( $attachment_id, $unfiltered = false ) { $file = get_post_meta( $attachment_id, '_wp_attached_file', true ); // If the file is relative, prepend upload dir. if ( $file && ! str_starts_with( $file, '/' ) && ! preg_match( '|^.:\\\|', $file ) ) { $uploads = wp_get_upload_dir(); if ( false === $uploads['error'] ) { $file = $uploads['basedir'] . "/$file"; } } if ( $unfiltered ) { return $file; } /** * Filters the attached file based on the given ID. * * @since 2.1.0 * * @param string|false $file The file path to where the attached file should be, false otherwise. * @param int $attachment_id Attachment ID. */ return apply_filters( 'get_attached_file', $file, $attachment_id );}Changelog
Introduced in 2.0.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.9.7 tag, from
src/wp-includes/post.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.