wppaste
WordPress

get_compat_media_markup( int $attachment_id, array $args = null ): array

Since
3.5.0
Source
wp-admin/includes/media.php:1874

Compatibility

WordPress
since 3.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
$argsarrayoptional
Default: null

Return value

array

Performance profile

How much work a call to get_compat_media_markup() 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
66–91

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

Plugin surface
3 hooks

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

Called by
2

2 places 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
  • cacheobject cachewp_cache_get()one call below get_compat_media_markup()

Further down the call graph this can also reach option, 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 · 4 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost get_compat_media_markup() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
!$args66–73get_post(), current_user_can(), wp_parse_args(), apply_filters(), array_merge_recursive(), apply_filters(), apply_filters()
!$args73–80get_post(), current_user_can(), wp_parse_args(), apply_filters(), array_merge_recursive(), apply_filters(), apply_filters(), wp_required_field_message()
!$args77–84get_post(), current_user_can(), wp_parse_args(), apply_filters(), array_merge_recursive(), apply_filters(), apply_filters(), esc_attr()
!$args84–91get_post(), current_user_can(), wp_parse_args(), apply_filters(), array_merge_recursive(), apply_filters(), apply_filters(), wp_required_field_message(), esc_attr()

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-dev35566–9146
8.535566–9146
8.435566–91466 fewer instructions than PHP 8.3
8.336166–9146
8.236166–9146
8.136166–91461 fewer instruction than PHP 7.4
7.436266–9146

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 get_compat_media_markup() runs, in this order:

  1. apply_filters( get_media_item_args )filterline 1887 (+13 into the body)

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

  2. apply_filters( attachment_fields_to_edit )filterline 1935 (+61 into the body)

    Filters the attachment fields to edit.

  3. apply_filters( media_meta )filterline 1950 (+76 into the body)

    Filters the media metadata.

Uses · 12

Used by · 2

Source code

function get_compat_media_markup( $attachment_id, $args = null ) {	$post = get_post( $attachment_id ); 	$default_args = array(		'errors'   => null,		'in_modal' => false,	); 	$user_can_edit = current_user_can( 'edit_post', $attachment_id ); 	$args = wp_parse_args( $args, $default_args ); 	/** This filter is documented in wp-admin/includes/media.php */	$args = apply_filters( 'get_media_item_args', $args ); 	$form_fields = array(); 	if ( $args['in_modal'] ) {		foreach ( get_attachment_taxonomies( $post ) as $taxonomy ) {			$t = (array) get_taxonomy( $taxonomy ); 			if ( ! $t['public'] || ! $t['show_ui'] ) {				continue;			} 			if ( empty( $t['label'] ) ) {				$t['label'] = $taxonomy;			} 			if ( empty( $t['args'] ) ) {				$t['args'] = array();			} 			$terms = get_object_term_cache( $post->ID, $taxonomy ); 			if ( false === $terms ) {				$terms = wp_get_object_terms( $post->ID, $taxonomy, $t['args'] );			} 			$values = array(); 			foreach ( $terms as $term ) {				$values[] = $term->slug;			} 			$t['value']    = implode( ', ', $values );			$t['taxonomy'] = true; 			$form_fields[ $taxonomy ] = $t;		}	} 	/*	 * Merge default fields with their errors, so any key passed with the error	 * (e.g. 'error', 'helps', 'value') will replace the default.	 * The recursive merge is easily traversed with array casting:	 * foreach ( (array) $things as $thing )	 */	$form_fields = array_merge_recursive( $form_fields, (array) $args['errors'] ); 	/** This filter is documented in wp-admin/includes/media.php */	$form_fields = apply_filters( 'attachment_fields_to_edit', $form_fields, $post ); 	unset(		$form_fields['image-size'],		$form_fields['align'],		$form_fields['image_alt'],		$form_fields['post_title'],		$form_fields['post_excerpt'],		$form_fields['post_content'],		$form_fields['url'],		$form_fields['menu_order'],		$form_fields['image_url']	); 	/** This filter is documented in wp-admin/includes/media.php */	$media_meta = apply_filters( 'media_meta', '', $post ); 	$defaults = array(		'input'         => 'text',

Changelog

Introduced in 3.5.0. One change between 6.7.7 and 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.

7.0.4
Return type changed from array to array<string,.verified against source
3.5.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.9.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.