wppaste
WordPress

get_media_item( int $attachment_id, string|array $args = null ): string

Since
2.5.0
Source
wp-admin/includes/media.php:1600
Retrieves HTML form for modifying the image attachment.

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

Reaches the database via get_post().

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
248–284

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

Plugin surface
2 hooks

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

Called by
1

1 place in core call this, so the cost is paid more often than your own code shows.

What it touches

  • querycontent queryget_post()called directly
  • hookthird-party callbacksapply_filters()called directly
  • optionoption read or writeget_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.

WhenInstructionsCalls 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–284get_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

PHPCompiledExecutedBranchesNotes
8.6-dev787248–284681 fewer instruction than PHP 8.5
8.5788248–28468
8.4788248–284683 fewer instructions than PHP 8.3
8.3791248–28468
8.2791248–28468
8.1791248–284684 fewer instructions than PHP 7.4
7.4795249–28568

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:

  1. apply_filters( get_media_item_args )filterline 1636 (+36 into the body)

    Filters the arguments used to retrieve an image for the edit image form.

  2. apply_filters( media_meta )filterline 1697 (+97 into the body)

    Filters the media metadata.

Uses · 32

Show all 32

Used by · 1

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, '&hellip;' ) . '</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.

  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.1.0 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.