Using WordPress ‘media_post_single_attachment_fields_to_edit()’ PHP function

The media_post_single_attachment_fields_to_edit() WordPress PHP function retrieves the post non-image attachment fields to edit form fields.

Usage

media_post_single_attachment_fields_to_edit($form_fields, $post);

Input:

  • $form_fields: An array of attachment form fields.
  • $post: The WP_Post attachment object.

Output: An array of attachment form fields.

Parameters

  • $form_fields (array) – An array of attachment form fields.
  • $post (WP_Post) – The WP_Post attachment object.

More information

See WordPress Developer Resources: media_post_single_attachment_fields_to_edit

Examples

Add custom fields to attachment form fields

In this example, we will add a custom field to the attachment form fields.

add_filter('attachment_fields_to_edit', 'add_custom_attachment_fields', 10, 2);

function add_custom_attachment_fields($form_fields, $post) {
    // Adding custom field 'custom_field_key' with a label and input element
    $form_fields['custom_field_key'] = array(
        'label' => 'Custom Field Label',
        'input' => 'text',
        'value' => get_post_meta($post->ID, 'custom_field_key', true),
    );

    return $form_fields;
}

Modify existing attachment form fields

In this example, we modify the label of the existing ‘post_excerpt’ field.

add_filter('attachment_fields_to_edit', 'modify_existing_attachment_fields', 10, 2);

function modify_existing_attachment_fields($form_fields, $post) {
    // Modifying the label of 'post_excerpt' field
    $form_fields['post_excerpt']['label'] = 'New Excerpt Label';

    return $form_fields;
}

Remove attachment form fields

In this example, we will remove the ‘url’ field from the attachment form fields.

add_filter('attachment_fields_to_edit', 'remove_attachment_fields', 10, 2);

function remove_attachment_fields($form_fields, $post) {
    // Removing the 'url' field from the attachment form fields
    unset($form_fields['url']);

    return $form_fields;
}

Change input type of an attachment form field

In this example, we will change the input type of the ‘post_content’ field to a ‘textarea’.

add_filter('attachment_fields_to_edit', 'change_input_type_attachment_fields', 10, 2);

function change_input_type_attachment_fields($form_fields, $post) {
    // Changing the input type of 'post_content' field to 'textarea'
    $form_fields['post_content']['input'] = 'textarea';

    return $form_fields;
}

Set a custom attribute for an attachment form field

In this example, we will set a custom attribute (data-custom-attribute) for the ‘post_title’ field.

add_filter('attachment_fields_to_edit', 'set_custom_attribute_attachment_fields', 10, 2);

function set_custom_attribute_attachment_fields($form_fields, $post) {
    // Setting a custom attribute 'data-custom-attribute' for the 'post_title' field
    $form_fields['post_title']['extra_rows'] = array(
        'data-custom-attribute' => 'Custom Attribute Value'
    );

    return $form_fields;
}