wppaste
WordPress

get_attached_file( int $attachment_id, bool $unfiltered = false ): string|false

Since
2.0.0
Source
wp-includes/post.php:825
Retrieves attached file path based on attachment ID.

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

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
11–30

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 31.

Plugin surface
1 hook

Third-party callbacks on 'get_attached_file' run inside this call, and their cost is not bounded by anything here.

Called by
25

25 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_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.

WhenInstructionsCalls it makes
always11–15get_post_meta()
always16–20get_post_meta(), apply_filters()
!$file21–25get_post_meta(), wp_get_upload_dir()
!$file26–30get_post_meta(), wp_get_upload_dir(), apply_filters()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev3111–305
8.53111–305
8.43111–3056 fewer instructions than PHP 8.3
8.33711–365
8.23711–365
8.13711–365
7.43711–365

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:

  1. apply_filters( get_attached_file )filterline 848 (+23 into the body)

    Filters the attached file based on the given ID.

Uses · 4

Used by · 25

Show all 25

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.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.7.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.