wp_image_file_matches_image_meta( string $image_location, array $image_meta, int $attachment_id = 0 ): bool
- Since
- 5.5.0
- Source
wp-includes/media.php:1670
Description
The image meta data is retrieved by attachment post ID. In some cases the post IDs may change.
For example when the website is exported and imported at another website. Then the attachment post IDs that are in post_content for the exported website may not match the same attachments at the new website.
Compatibility
- WordPress
- since 5.5.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
$image_locationstring- The full path or URI to the image file.
$image_metaarray- The attachment meta data as returned by 'wp_get_attachment_metadata()'.
$attachment_idintoptional- The image attachment ID. Default 0.Default:
0
Return value
bool- Whether the image meta is for this image file.
Performance profile
How much work a call to wp_image_file_matches_image_meta() 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
- Light
- Scaling
- Scales with input
- Instructions
- 14–81
- Plugin surface
- 1 hook
- Called by
- 1
Touches nothing outside its own arguments.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 83.
Third-party callbacks on 'wp_image_file_matches_image_meta' run inside this call, and their cost is not bounded by anything here.
1 place 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
What one call costs · 8 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_image_file_matches_image_meta() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 14–18 | apply_filters() |
isset($image_meta) | 38 | explode(), strrpos(), apply_filters() |
isset($image_meta) && empty($image_meta) | 46–52 | explode(), strrpos(), _wp_get_attachment_relative_path(), apply_filters() |
isset($image_meta) && empty($image_meta) | 50–56 | explode(), strrpos(), _wp_get_attachment_relative_path(), trailingslashit(), apply_filters() |
isset($image_meta) && !empty($image_meta) | 57–65 | explode(), strrpos(), _wp_get_attachment_relative_path(), strrpos(), apply_filters() |
isset($image_meta) && !empty($image_meta) | 61–69 | explode(), strrpos(), _wp_get_attachment_relative_path(), trailingslashit(), strrpos(), apply_filters() |
isset($image_meta) && !empty($image_meta) && !$image_meta | 76–77 | explode(), strrpos(), _wp_get_attachment_relative_path(), strrpos(), strrpos(), apply_filters() |
isset($image_meta) && !empty($image_meta) && !$image_meta | 80–81 | explode(), strrpos(), _wp_get_attachment_relative_path(), trailingslashit(), strrpos(), strrpos(), apply_filters() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 83 instructions, 14–81 executed per call, 11 branches. The work does not change between versions.
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 wp_image_file_matches_image_meta() runs, in this order:
- apply_filters( wp_image_file_matches_image_meta )filterline 1721 (+51 into the body)
Filters whether an image path or URI matches image meta.
Uses · 3
- _wp_get_attachment_relative_path()Gets the attachment path relative to the upload directory.
- trailingslashit()Appends a trailing slash.
- apply_filters()Calls the callback functions that have been added to a filter hook.
Used by · 1
- WP_REST_Attachments_Controller::edit_media_item()Applies edits to a media item and creates a new attachment record.
Source code
function wp_image_file_matches_image_meta( $image_location, $image_meta, $attachment_id = 0 ) { $match = false; // Ensure the $image_meta is valid. if ( isset( $image_meta['file'] ) && strlen( $image_meta['file'] ) > 4 ) { // Remove query args in image URI. list( $image_location ) = explode( '?', $image_location ); // Check if the relative image path from the image meta is at the end of $image_location. if ( strrpos( $image_location, $image_meta['file'] ) === strlen( $image_location ) - strlen( $image_meta['file'] ) ) { $match = true; } else { // Retrieve the uploads sub-directory from the full size image. $dirname = _wp_get_attachment_relative_path( $image_meta['file'] ); if ( $dirname ) { $dirname = trailingslashit( $dirname ); } if ( ! empty( $image_meta['original_image'] ) ) { $relative_path = $dirname . $image_meta['original_image']; if ( strrpos( $image_location, $relative_path ) === strlen( $image_location ) - strlen( $relative_path ) ) { $match = true; } } if ( ! $match && ! empty( $image_meta['sizes'] ) ) { foreach ( $image_meta['sizes'] as $image_size_data ) { $relative_path = $dirname . $image_size_data['file']; if ( strrpos( $image_location, $relative_path ) === strlen( $image_location ) - strlen( $relative_path ) ) { $match = true; break; } } } } } /** * Filters whether an image path or URI matches image meta. * * @since 5.5.0 * * @param bool $match Whether the image relative path from the image meta * matches the end of the URI or path to the image file. * @param string $image_location Full path or URI to the tested image file. * @param array $image_meta The image meta data as returned by 'wp_get_attachment_metadata()'. * @param int $attachment_id The image attachment ID or 0 if not supplied. */ return apply_filters( 'wp_image_file_matches_image_meta', $match, $image_location, $image_meta, $attachment_id );}Changelog
Introduced in 5.5.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 7.0.4 tag, from
src/wp-includes/media.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.