Title: get_attachment_fields_to_edit
Published: April 25, 2014
Last modified: May 20, 2026

---

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

## In this article

 * [Parameters](https://developer.wordpress.org/reference/functions/get_attachment_fields_to_edit/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/get_attachment_fields_to_edit/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/get_attachment_fields_to_edit/?output_format=md#source)
 * [Hooks](https://developer.wordpress.org/reference/functions/get_attachment_fields_to_edit/?output_format=md#hooks)
 * [Related](https://developer.wordpress.org/reference/functions/get_attachment_fields_to_edit/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/get_attachment_fields_to_edit/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/functions/get_attachment_fields_to_edit/?output_format=md#wp--skip-link--target)

Retrieves the attachment fields to edit form fields.

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/get_attachment_fields_to_edit/?output_format=md#parameters)󠁿

 `$post`[WP_Post](https://developer.wordpress.org/reference/classes/wp_post/)required

`$errors`arrayoptional

Default:`null`

## 󠀁[Return](https://developer.wordpress.org/reference/functions/get_attachment_fields_to_edit/?output_format=md#return)󠁿

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

## 󠀁[Source](https://developer.wordpress.org/reference/functions/get_attachment_fields_to_edit/?output_format=md#source)󠁿

    ```php
    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;
    	}

    	/*
    	 * 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) $errors );

    	// This was formerly in image_attachment_fields_to_edit().
    	if ( str_starts_with( $post->post_mime_type, 'image' ) ) {
    		$alt = get_post_meta( $post->ID, '_wp_attachment_image_alt', true );

    		if ( empty( $alt ) ) {
    			$alt = '';
    		}

    		$form_fields['post_title']['required'] = true;

    		$form_fields['image_alt'] = array(
    			'value' => $alt,
    			'label' => __( 'Alternative Text' ),
    			'helps' => __( 'Alt text for the image, e.g. &#8220;The Mona Lisa&#8221;' ),
    		);

    		$form_fields['align'] = array(
    			'label' => __( 'Alignment' ),
    			'input' => 'html',
    			'html'  => image_align_input_fields( $post, get_option( 'image_default_align' ) ),
    		);

    		$form_fields['image-size'] = image_size_input_fields( $post, get_option( 'image_default_size', 'medium' ) );

    	} else {
    		unset( $form_fields['image_alt'] );
    	}

    	/**
    	 * Filters the attachment fields to edit.
    	 *
    	 * @since 2.5.0
    	 *
    	 * @param array   $form_fields An array of attachment form fields.
    	 * @param WP_Post $post        The WP_Post attachment object.
    	 */
    	$form_fields = apply_filters( 'attachment_fields_to_edit', $form_fields, $post );

    	return $form_fields;
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-admin/includes/media.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.0/src/wp-admin/includes/media.php#L1402)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.0/src/wp-admin/includes/media.php#L1402-L1529)

## 󠀁[Hooks](https://developer.wordpress.org/reference/functions/get_attachment_fields_to_edit/?output_format=md#hooks)󠁿

 [apply_filters( ‘attachment_fields_to_edit’, array $form_fields, WP_Post $post )](https://developer.wordpress.org/reference/hooks/attachment_fields_to_edit/)

Filters the attachment fields to edit.

## 󠀁[Related](https://developer.wordpress.org/reference/functions/get_attachment_fields_to_edit/?output_format=md#related)󠁿

| Uses | Description | 
| [wp_caption_input_textarea()](https://developer.wordpress.org/reference/functions/wp_caption_input_textarea/)`wp-admin/includes/media.php` |

Outputs a textarea element for inputting an attachment caption.

  | 
| [image_link_input_fields()](https://developer.wordpress.org/reference/functions/image_link_input_fields/)`wp-admin/includes/media.php` |

Retrieves HTML for the Link URL buttons with the default link type as specified.

  | 
| [image_align_input_fields()](https://developer.wordpress.org/reference/functions/image_align_input_fields/)`wp-admin/includes/media.php` |

Retrieves HTML for the image alignment radio buttons with the specified one checked.

  | 
| [image_size_input_fields()](https://developer.wordpress.org/reference/functions/image_size_input_fields/)`wp-admin/includes/media.php` |

Retrieves HTML for the size radio buttons with the specified one checked.

  | 
| [get_object_term_cache()](https://developer.wordpress.org/reference/functions/get_object_term_cache/)`wp-includes/taxonomy.php` |

Retrieves the cached term objects for the given object ID.

  | 
| [wp_get_object_terms()](https://developer.wordpress.org/reference/functions/wp_get_object_terms/)`wp-includes/taxonomy.php` |

Retrieves the terms associated with the given object(s), in the supplied taxonomies.

  | 
| [get_attachment_taxonomies()](https://developer.wordpress.org/reference/functions/get_attachment_taxonomies/)`wp-includes/media.php` |

Retrieves taxonomies attached to given the attachment.

  | 
| [WP_Post::__construct()](https://developer.wordpress.org/reference/classes/wp_post/__construct/)`wp-includes/class-wp-post.php` |

Constructor.

  | 
| [wp_get_attachment_url()](https://developer.wordpress.org/reference/functions/wp_get_attachment_url/)`wp-includes/post.php` |

Retrieves the URL for an attachment.

  | 
| [sanitize_post()](https://developer.wordpress.org/reference/functions/sanitize_post/)`wp-includes/post.php` |

Sanitizes every post field.

  | 
| [__()](https://developer.wordpress.org/reference/functions/__/)`wp-includes/l10n.php` |

Retrieves the translation of $text.

  | 
| [esc_attr()](https://developer.wordpress.org/reference/functions/esc_attr/)`wp-includes/formatting.php` |

Escaping for HTML attributes.

  | 
| [get_taxonomy()](https://developer.wordpress.org/reference/functions/get_taxonomy/)`wp-includes/taxonomy.php` |

Retrieves the taxonomy object of $taxonomy.

  | 
| [apply_filters()](https://developer.wordpress.org/reference/functions/apply_filters/)`wp-includes/plugin.php` |

Calls the callback functions that have been added to a filter hook.

  | 
| [get_option()](https://developer.wordpress.org/reference/functions/get_option/)`wp-includes/option.php` |

Retrieves an option value based on an option name.

  | 
| [get_post_meta()](https://developer.wordpress.org/reference/functions/get_post_meta/)`wp-includes/post.php` |

Retrieves a post meta field for the given post ID.

  | 
| [get_post()](https://developer.wordpress.org/reference/functions/get_post/)`wp-includes/post.php` |

Retrieves post data given a post ID or post object.

  |

[Show 12 more](https://developer.wordpress.org/reference/functions/get_attachment_fields_to_edit/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/get_attachment_fields_to_edit/?output_format=md#)

| Used by | Description | 
| [get_media_item()](https://developer.wordpress.org/reference/functions/get_media_item/)`wp-admin/includes/media.php` |

Retrieves HTML form for modifying the image attachment.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/get_attachment_fields_to_edit/?output_format=md#changelog)󠁿

| Version | Description | 
| [2.5.0](https://developer.wordpress.org/reference/since/2.5.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_attachment_fields_to_edit%2F)
before being able to contribute a note or feedback.