get_media_item( int $attachment_id, string|array $args = null ): string
- Since
- 2.5.0
- Source
wp-admin/includes/media.php:1582
Compatibility
- WordPress
- since 2.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
$attachment_idint- Attachment ID for modification.
$argsstring|arrayoptional- Override defaults.Default:
null
Return value
string- HTML form for attachment.
Performance profile
How much work a call to get_media_item() 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
- Scales with input
- Instructions
- 248–284
- Plugin surface
- 2 hooks
- Called by
- 1
Reaches the database via get_post().
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 788.
Third-party callbacks on 'get_media_item_args', 'media_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
- querycontent query
get_post()called directly - hookthird-party callbacks
apply_filters()called directly - optionoption read or write
get_option()one call below get_media_item()
Further down the call graph this can also reach 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 · 1 distinct outcome
One number would be a lie: the work depends on which branch runs. These are every distinct cost get_media_item() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
empty($value) && !$parsed_args && empty($title) && !isset($value) && !isset($redir_tab) && !isset($meta) && !wp_attachment_is_image() && empty($media_dims) && !empty($parsed_args) && $type !== "image" | 248–284 | get_post(), wp_parse_args(), apply_filters(), __(), __(), get_attached_file(), wp_basename(), esc_html(), esc_attr(), get_post_mime_types(), array_keys(), wp_match_mime_types(), array_keys(), reset(), esc_attr(), get_attachment_fields_to_edit(), wp_get_attachment_metadata(), apply_filters(), wp_attachment_is_image(), get_permalink(), __(), __(), __(), __(), mysql2date(), wp_required_field_message() |
This body has more branch combinations than are worth enumerating, so the table covers the outcomes found first rather than every one that exists.
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 787 | 248–284 | 68 | 1 fewer instruction than PHP 8.5 |
| 8.5 | 788 | 248–284 | 68 | |
| 8.4 | 788 | 248–284 | 68 | 3 fewer instructions than PHP 8.3 |
| 8.3 | 791 | 248–284 | 68 | |
| 8.2 | 791 | 248–284 | 68 | |
| 8.1 | 791 | 248–284 | 68 | 4 fewer instructions than PHP 7.4 |
| 7.4 | 795 | 249–285 | 68 |
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 · 2
2 hooks fire while get_media_item() runs, in this order:
- apply_filters( get_media_item_args )filterline 1618 (+36 into the body)
Filters the arguments used to retrieve an image for the edit image form.
Uses · 32
- wp_get_attachment_image_src()Retrieves an image to represent an attachment.
- get_post()Retrieves post data given a post ID or post object.
- post_type_supports()Checks a post type's support for a given feature.
- get_post_type()Retrieves the post type of the current post or of a given post.
- wp_parse_args()Merges user defined arguments into defaults array.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- __()Retrieves the translation of $text.
- get_attached_file()Retrieves attached file path based on attachment ID.
- esc_html()Escaping for HTML blocks.
- wp_basename()i18n-friendly version of basename().
- esc_attr()Escaping for HTML attributes.
- get_post_mime_types()Gets default post mime types.
Show all 32
- wp_match_mime_types()Checks a MIME-Type against a list.
- get_attachment_fields_to_edit()Retrieves the attachment fields to edit form fields.
- wp_html_excerpt()Safely extracts not more than the first $count characters from HTML string.
- wp_get_attachment_metadata()Retrieves attachment metadata for attachment ID.
- wp_attachment_is_image()Determines whether an attachment is an image.
- wp_image_editor_supports()Tests whether there is an editor that supports a given mime type or methods.
- wp_create_nonce()Creates a cryptographic token tied to a specific action, user, user session, and window of time.
- esc_attr__()Retrieves the translation of $text and escapes it for safe use in an attribute.
- get_permalink()Retrieves the full permalink for the current post or post ID.
- mysql2date()Converts given MySQL date string into a different format.
- wp_required_field_message()Creates a message to explain required form fields.
- get_submit_button()Returns a submit button, with provided text and appropriate class.
- current_user_can()Returns whether the current user has the specified capability.
- wp_nonce_url()Retrieves URL with nonce added to URL query.
- absint()Converts a value to non-negative integer.
- current_theme_supports()Checks a theme's support for a given feature.
- get_post_thumbnail_id()Retrieves the post thumbnail ID.
- get_post_type_object()Retrieves a post type object by name.
- wp_required_field_indicator()Assigns a visual indicator for required form fields.
- user_can_richedit()Determines whether the user can access the visual editor.
Used by · 1
- get_media_items()Retrieves HTML for media items of post gallery.
Source code
function get_media_item( $attachment_id, $args = null ) { global $redir_tab; $thumb_url = false; $attachment_id = (int) $attachment_id; if ( $attachment_id ) { $thumb_url = wp_get_attachment_image_src( $attachment_id, 'thumbnail', true ); if ( $thumb_url ) { $thumb_url = $thumb_url[0]; } } $post = get_post( $attachment_id ); $current_post_id = ! empty( $_GET['post_id'] ) ? (int) $_GET['post_id'] : 0; $default_args = array( 'errors' => null, 'send' => $current_post_id ? post_type_supports( get_post_type( $current_post_id ), 'editor' ) : true, 'delete' => true, 'toggle' => true, 'show_title' => true, ); $parsed_args = wp_parse_args( $args, $default_args ); /** * Filters the arguments used to retrieve an image for the edit image form. * * @since 3.1.0 * * @see get_media_item * * @param array $parsed_args An array of arguments. */ $parsed_args = apply_filters( 'get_media_item_args', $parsed_args ); $toggle_on = __( 'Show' ); $toggle_off = __( 'Hide' ); $file = get_attached_file( $post->ID ); $filename = esc_html( wp_basename( $file ) ); $title = esc_attr( $post->post_title ); $post_mime_types = get_post_mime_types(); $keys = array_keys( wp_match_mime_types( array_keys( $post_mime_types ), $post->post_mime_type ) ); $type = reset( $keys ); $type_html = "<input type='hidden' id='type-of-$attachment_id' value='" . esc_attr( $type ) . "' />"; $form_fields = get_attachment_fields_to_edit( $post, $parsed_args['errors'] ); if ( $parsed_args['toggle'] ) { $class = empty( $parsed_args['errors'] ) ? 'startclosed' : 'startopen'; $toggle_links = " <a class='toggle describe-toggle-on' href='#'>$toggle_on</a> <a class='toggle describe-toggle-off' href='#'>$toggle_off</a>"; } else { $class = ''; $toggle_links = ''; } $display_title = ( ! empty( $title ) ) ? $title : $filename; // $title shouldn't ever be empty, but just in case. $display_title = $parsed_args['show_title'] ? "<div class='filename new'><span class='title'>" . wp_html_excerpt( $display_title, 60, '…' ) . '</span></div>' : ''; $gallery = ( ( isset( $_REQUEST['tab'] ) && 'gallery' === $_REQUEST['tab'] ) || ( isset( $redir_tab ) && 'gallery' === $redir_tab ) ); $order = ''; foreach ( $form_fields as $key => $val ) { if ( 'menu_order' === $key ) { if ( $gallery ) { $order = "<div class='menu_order'> <input class='menu_order_input' type='text' id='attachments[$attachment_id][menu_order]' name='attachments[$attachment_id][menu_order]' value='" . esc_attr( $val['value'] ) . "' /></div>"; } else { $order = "<input type='hidden' name='attachments[$attachment_id][menu_order]' value='" . esc_attr( $val['value'] ) . "' />"; } unset( $form_fields['menu_order'] ); break; } }Changelog
Introduced in 2.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 6.7.7 tag, from
src/wp-admin/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.