Using WordPress ‘image_attachment_fields_to_edit()’ PHP function

The image_attachment_fields_to_edit() WordPress PHP function retrieves the image attachment fields to edit form fields.

Usage

image_attachment_fields_to_edit($form_fields, $post);

Custom example:

Input:

$form_fields = array();
$post = get_post(123);
$image_fields = image_attachment_fields_to_edit($form_fields, $post);

Output: An array containing the image attachment fields to edit.

Parameters

  • $form_fields (array) – Required. The form fields to include in the array.
  • $post (object) – Required. The post object for the image attachment.

More information

See WordPress Developer Resources: image_attachment_fields_to_edit

Examples

Displaying image attachment form fields

This example retrieves the form fields for a specific image attachment and displays them in a table.

// Get the post object for the image attachment
$post = get_post(123);

// Prepare the form fields array
$form_fields = array();

// Retrieve the image attachment form fields
$image_fields = image_attachment_fields_to_edit($form_fields, $post);

// Display the form fields in a table
echo '<table>';
foreach ($image_fields as $key => $field) {
    echo '<tr>';
    echo '<td>' . $field['label'] . '</td>';
    echo '<td>' . $field['input'] . '</td>';
    echo '</tr>';
}
echo '</table>';

Adding a custom field to the image attachment form fields

This example adds a custom field to the image attachment form fields.

// Get the post object for the image attachment
$post = get_post(123);

// Prepare the form fields array with a custom field
$form_fields = array(
    'custom_field' => array(
        'label' => __('Custom Field', 'textdomain'),
        'input' => 'text',
        'value' => get_post_meta($post->ID, '_custom_field', true),
    ),
);

// Retrieve the image attachment form fields
$image_fields = image_attachment_fields_to_edit($form_fields, $post);

Removing a specific field from the image attachment form fields

This example removes the ‘Caption’ field from the image attachment form fields.

// Get the post object for the image attachment
$post = get_post(123);

// Prepare the form fields array
$form_fields = array();

// Retrieve the image attachment form fields
$image_fields = image_attachment_fields_to_edit($form_fields, $post);

// Remove the 'Caption' field
unset($image_fields['post_excerpt']);

Modifying an existing field in the image attachment form fields

This example modifies the ‘Alt Text’ field’s label in the image attachment form fields.

// Get the post object for the image attachment
$post = get_post(123);

// Prepare the form fields array
$form_fields = array();

// Retrieve the image attachment form fields
$image_fields = image_attachment_fields_to_edit($form_fields, $post);

// Modify the 'Alt Text' field label
$image_fields['_wp_attachment_image_alt']['label'] = __('Custom Alt Text Label', 'textdomain');

Retrieving the values of the image attachment form fields

This example retrieves the values of the image attachment form fields for further processing.

// Get the post object for the image attachment
$post = get_post(123);

// Prepare the form fields array
$form_fields = array();

// Retrieve the image attachment form fields
$image_fields = image_attachment_fields_to_edit