wppaste
WordPress

_load_image_to_edit_path( int $attachment_id, string|int[] $size = 'full' ): string|false

Since
3.4.0
Source
wp-admin/includes/image.php:1270
Retrieves the path or URL of an attachment's attached file.

Description

If the attached file is not present on the local filesystem (usually due to replication plugins), then the URL of the file is returned if allow_url_fopen is supported.

Compatibility

WordPress
since 3.4.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.
$sizestring|int[]optional
Image size. Accepts any registered image size name, or an array of width and height values in pixels (in that order). Default 'full'.Default: 'full'

Return value

string|false
File path or URL on success, false on failure.

Performance profile

How much work a call to _load_image_to_edit_path() 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
Heavy

Reads or writes the filesystem via file_exists().

Scaling
Constant

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

Instructions
20–42

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

Plugin surface
3 hooks

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

Called by
6

6 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
  • filesystemfilesystem accessfile_exists()called directly
  • querycontent queryget_post()one call below _load_image_to_edit_path()

Further down the call graph this can also reach option, cache, serialize 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 · 5 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost _load_image_to_edit_path() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
file_exists() && $size === "full"20get_attached_file(), file_exists(), apply_filters()
always24get_attached_file(), wp_get_attachment_url(), apply_filters(), apply_filters()
file_exists() && $size !== "full"26get_attached_file(), file_exists(), image_get_intermediate_size(), apply_filters()
!file_exists()28get_attached_file(), file_exists(), wp_get_attachment_url(), apply_filters(), apply_filters()
file_exists() && $size !== "full"42get_attached_file(), file_exists(), image_get_intermediate_size(), path_join(), apply_filters(), apply_filters()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev5220–424
8.55220–424
8.45220–4242 fewer instructions than PHP 8.3
8.35420–444
8.25420–444
8.15420–444
7.45420–444

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 · 3

3 hooks fire while _load_image_to_edit_path() runs, in this order:

  1. apply_filters( load_image_to_edit_filesystempath )filterline 1292 (+22 into the body)

    Filters the path to an attachment's file when editing the image.

  2. apply_filters( load_image_to_edit_attachmenturl )filterline 1308 (+38 into the body)

    Filters the path to an attachment's URL when editing the image.

  3. apply_filters( load_image_to_edit_path )filterline 1321 (+51 into the body)

    Filters the returned path or URL of the current image.

Uses · 5

Used by · 6

Source code

function _load_image_to_edit_path( $attachment_id, $size = 'full' ) {	$filepath = get_attached_file( $attachment_id ); 	if ( $filepath && file_exists( $filepath ) ) {		if ( 'full' !== $size ) {			$data = image_get_intermediate_size( $attachment_id, $size ); 			if ( $data ) {				$filepath = path_join( dirname( $filepath ), $data['file'] ); 				/**				 * Filters the path to an attachment's file when editing the image.				 *				 * The filter is evaluated for all image sizes except 'full'.				 *				 * @since 3.1.0				 *				 * @param string       $path          Path to the current image.				 * @param int          $attachment_id Attachment ID.				 * @param string|int[] $size          Requested image size. Can be any registered image size name, or				 *                                    an array of width and height values in pixels (in that order).				 */				$filepath = apply_filters( 'load_image_to_edit_filesystempath', $filepath, $attachment_id, $size );			}		}	} elseif ( function_exists( 'fopen' ) && ini_get( 'allow_url_fopen' ) ) {		/**		 * Filters the path to an attachment's URL when editing the image.		 *		 * The filter is only evaluated if the file isn't stored locally and `allow_url_fopen` is enabled on the server.		 *		 * @since 3.1.0		 *		 * @param string|false $image_url     Current image URL.		 * @param int          $attachment_id Attachment ID.		 * @param string|int[] $size          Requested image size. Can be any registered image size name, or		 *                                    an array of width and height values in pixels (in that order).		 */		$filepath = apply_filters( 'load_image_to_edit_attachmenturl', wp_get_attachment_url( $attachment_id ), $attachment_id, $size );	} 	/**	 * Filters the returned path or URL of the current image.	 *	 * @since 2.9.0	 *	 * @param string|false $filepath      File path or URL to current image, or false.	 * @param int          $attachment_id Attachment ID.	 * @param string|int[] $size          Requested image size. Can be any registered image size name, or	 *                                    an array of width and height values in pixels (in that order).	 */	return apply_filters( 'load_image_to_edit_path', $filepath, $attachment_id, $size );}

Changelog

Introduced in 3.4.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 7.0.4 tag, from src/wp-admin/includes/image.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.