_load_image_to_edit_path( int $attachment_id, string|int[] $size = 'full' ): string|false
- Since
- 3.4.0
- Source
wp-admin/includes/image.php:1196
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
- Scaling
- Constant
- Instructions
- 20–42
- Plugin surface
- 3 hooks
- Called by
- 6
Reads or writes the filesystem via file_exists().
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 52.
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.
6 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 - filesystemfilesystem access
file_exists()called directly - querycontent query
get_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.
| When | Instructions | Calls it makes |
|---|---|---|
file_exists() && $size === "full" | 20 | get_attached_file(), file_exists(), apply_filters() |
| always | 24 | get_attached_file(), wp_get_attachment_url(), apply_filters(), apply_filters() |
file_exists() && $size !== "full" | 26 | get_attached_file(), file_exists(), image_get_intermediate_size(), apply_filters() |
!file_exists() | 28 | get_attached_file(), file_exists(), wp_get_attachment_url(), apply_filters(), apply_filters() |
file_exists() && $size !== "full" | 42 | get_attached_file(), file_exists(), image_get_intermediate_size(), path_join(), apply_filters(), apply_filters() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 52 | 20–42 | 4 | |
| 8.5 | 52 | 20–42 | 4 | |
| 8.4 | 52 | 20–42 | 4 | 2 fewer instructions than PHP 8.3 |
| 8.3 | 54 | 20–44 | 4 | |
| 8.2 | 54 | 20–44 | 4 | |
| 8.1 | 54 | 20–44 | 4 | |
| 7.4 | 54 | 20–44 | 4 |
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:
- apply_filters( load_image_to_edit_filesystempath )filterline 1218 (+22 into the body)
Filters the path to an attachment's file when editing the image.
- apply_filters( load_image_to_edit_attachmenturl )filterline 1234 (+38 into the body)
Filters the path to an attachment's URL when editing the image.
- apply_filters( load_image_to_edit_path )filterline 1247 (+51 into the body)
Filters the returned path or URL of the current image.
Uses · 5
- get_attached_file()Retrieves attached file path based on attachment ID.
- image_get_intermediate_size()Retrieves the image's intermediate size (resized) path, width, and height.
- path_join()Joins two filesystem paths together.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- wp_get_attachment_url()Retrieves the URL for an attachment.
Used by · 6
- WP_REST_Attachments_Controller::edit_media_item()Applies edits to a media item and creates a new attachment record.
- _copy_image_file()Copies an existing image file.
- load_image_to_edit()Loads an image resource for editing.
- stream_preview_image()Streams image in post to browser, along with enqueued changes in `$_REQUEST['history']`.
- wp_crop_image()Crops an image to a given size.
- wp_save_image()Saves image to post, along with enqueued changes in `$_REQUEST['history']`.
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.
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-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.