wppaste
WordPress

get_attachment_fields_to_edit( WP_Post $post, array $errors = null ): array<string,

Since
2.5.0
Source
wp-admin/includes/media.php:1402
Retrieves the attachment fields to edit form fields.

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

$postWP_Post
$errorsarrayoptional
Default: null

Return value

array<string,
array<string, mixed>> The attachment fields.

Performance profile

How much work a call to get_attachment_fields_to_edit() 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
117–177

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

Plugin surface
1 hook

Third-party callbacks on 'attachment_fields_to_edit' 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
  • optionoption read or writeget_option()called directly
  • hookthird-party callbacksapply_filters()called directly
  • cacheobject cachewp_cache_get()one call below get_attachment_fields_to_edit()
  • serializeserialisationmaybe_unserialize()one call below get_attachment_fields_to_edit()

Further down the call graph this can also reach transient. That is 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_attachment_fields_to_edit() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
!is_int($post)117–123wp_get_attachment_url(), sanitize_post(), __(), __(), wp_caption_input_textarea(), __(), __(), get_option(), image_link_input_fields(), __(), __(), __(), esc_attr(), wp_get_attachment_url(), __(), get_attachment_taxonomies(), array_merge_recursive(), apply_filters()
is_int($post)121–127get_post(), wp_get_attachment_url(), sanitize_post(), __(), __(), wp_caption_input_textarea(), __(), __(), get_option(), image_link_input_fields(), __(), __(), __(), esc_attr(), wp_get_attachment_url(), __(), get_attachment_taxonomies(), array_merge_recursive(), apply_filters()
!is_int($post)166–173wp_get_attachment_url(), sanitize_post(), __(), __(), wp_caption_input_textarea(), __(), __(), get_option(), image_link_input_fields(), __(), __(), __(), esc_attr(), wp_get_attachment_url(), __(), get_attachment_taxonomies(), array_merge_recursive(), get_post_meta(), __(), __(), __(), get_option(), image_align_input_fields(), get_option(), image_size_input_fields(), apply_filters()
is_int($post)170–177get_post(), wp_get_attachment_url(), sanitize_post(), __(), __(), wp_caption_input_textarea(), __(), __(), get_option(), image_link_input_fields(), __(), __(), __(), esc_attr(), wp_get_attachment_url(), __(), get_attachment_taxonomies(), array_merge_recursive(), get_post_meta(), __(), __(), __(), get_option(), image_align_input_fields(), get_option(), image_size_input_fields(), apply_filters()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev228117–17713
8.5228117–17713
8.4228117–177136 fewer instructions than PHP 8.3
8.3234120–18013
8.2234120–18013
8.1234120–180131 fewer instruction than PHP 7.4
7.4235121–18113

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

  1. apply_filters( attachment_fields_to_edit )filterline 1526 (+124 into the body)

    Filters the attachment fields to edit.

Uses · 18

Show all 18

Used by · 1

Source code

function get_attachment_fields_to_edit( $post, $errors = null ) {	if ( is_int( $post ) ) {		$post = get_post( $post );	} 	if ( is_array( $post ) ) {		$post = new WP_Post( (object) $post );	} 	$image_url = wp_get_attachment_url( $post->ID ); 	$edit_post = sanitize_post( $post, 'edit' ); 	$form_fields = array(		'post_title'   => array(			'label' => __( 'Title' ),			'value' => $edit_post->post_title,		),		'image_alt'    => array(),		'post_excerpt' => array(			'label' => __( 'Caption' ),			'input' => 'html',			'html'  => wp_caption_input_textarea( $edit_post ),		),		'post_content' => array(			'label' => __( 'Description' ),			'value' => $edit_post->post_content,			'input' => 'textarea',		),		'url'          => array(			'label' => __( 'Link URL' ),			'input' => 'html',			'html'  => image_link_input_fields( $post, get_option( 'image_default_link_type' ) ),			'helps' => __( 'Enter a link URL or click above for presets.' ),		),		'menu_order'   => array(			'label' => __( 'Order' ),			'value' => $edit_post->menu_order,		),		'image_url'    => array(			'label' => __( 'File URL' ),			'input' => 'html',			'html'  => "<input type='text' class='text urlfield' readonly='readonly' name='attachments[$post->ID][url]' value='" . esc_attr( $image_url ) . "' /><br />",			'value' => wp_get_attachment_url( $post->ID ),			'helps' => __( 'Location of the uploaded file.' ),		),	); 	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 ); 		$form_fields[ $taxonomy ] = $t;	}

Changelog

Introduced in 2.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
2.5.0
Introduced.from the docblock

About this page

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