wp_maybe_generate_attachment_metadata( WP_Post $attachment )
- Since
- 3.9.0
- Source
wp-includes/media.php:5458
Compatibility
- WordPress
- since 3.9.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
$attachmentWP_Post- Attachment object.
Performance profile
How much work a call to wp_maybe_generate_attachment_metadata() 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
- 4–48
- Plugin surface
- None
- Called by
- 1
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 49.
Nothing here hands control to plugin code.
1 place in core call this, so the cost is paid more often than your own code shows.
What it touches
- transienttransient
get_transient()called directly - filesystemfilesystem access
file_exists()called directly - hookthird-party callbacks
apply_filters()one call below wp_maybe_generate_attachment_metadata() - querycontent query
get_post()one call below wp_maybe_generate_attachment_metadata() - cacheobject cache
wp_cache_get()one call below wp_maybe_generate_attachment_metadata() - optionoption read or write
get_option()one call below wp_maybe_generate_attachment_metadata()
Further down the call graph this can also reach serialize. That is the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 6 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_maybe_generate_attachment_metadata() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
empty($attachment) | 4–6 | none |
!empty($attachment) && !empty($meta) | 18 | get_attached_file(), wp_get_attachment_metadata() |
!empty($attachment) && empty($meta) && !file_exists() | 22 | get_attached_file(), wp_get_attachment_metadata(), file_exists() |
!empty($attachment) && empty($meta) && file_exists() && $_meta | 29 | get_attached_file(), wp_get_attachment_metadata(), file_exists(), get_post_meta() |
!empty($attachment) && empty($meta) && file_exists() && !$_meta && get_transient() | 33 | get_attached_file(), wp_get_attachment_metadata(), file_exists(), get_post_meta(), get_transient() |
!empty($attachment) && empty($meta) && file_exists() && !$_meta && !get_transient() | 48 | get_attached_file(), wp_get_attachment_metadata(), file_exists(), get_post_meta(), get_transient(), set_transient(), wp_generate_attachment_metadata(), wp_update_attachment_metadata(), delete_transient() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 49 instructions, 4–48 executed per call, 6 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.
Uses · 8
- get_attached_file()Retrieves attached file path based on attachment ID.
- wp_get_attachment_metadata()Retrieves attachment metadata for attachment ID.
- get_post_meta()Retrieves a post meta field for the given post ID.
- get_transient()Retrieves the value of a transient.
- set_transient()Sets/updates the value of a transient.
- wp_update_attachment_metadata()Updates metadata for an attachment.
- wp_generate_attachment_metadata()Generates attachment meta data and create image sub-sizes for images.
- delete_transient()Deletes a transient.
Used by · 1
- edit_form_image_editor()Displays the image and editor in the post editor
Source code
function wp_maybe_generate_attachment_metadata( $attachment ) { if ( empty( $attachment ) || empty( $attachment->ID ) ) { return; } $attachment_id = (int) $attachment->ID; $file = get_attached_file( $attachment_id ); $meta = wp_get_attachment_metadata( $attachment_id ); if ( empty( $meta ) && file_exists( $file ) ) { $_meta = get_post_meta( $attachment_id ); $_lock = 'wp_generating_att_' . $attachment_id; if ( ! array_key_exists( '_wp_attachment_metadata', $_meta ) && ! get_transient( $_lock ) ) { set_transient( $_lock, $file ); wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $file ) ); delete_transient( $_lock ); } }}Changelog
Introduced in 3.9.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.8.8 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.