Random WordPress Function

Learn about a new WordPress function every day!


Function Signature:

media_post_single_attachment_fields_to_edit

Function Description:

Retrieves the post non-image attachment fields to edit form fields.

Function Examples:

⚠️ Examples below are generated with GPT-3 once every hour. Do not take them too seriously.
Consider them as some extra input in your learning process - reason about them. Will it work? What could fail?
// Example 1: Adding a custom field to the single attachment edit screen
function custom_attachment_field( $form_fields, $post ) {
    $form_fields['custom_field'] = array(
        'label' => 'Custom Field',
        'input' => 'text',
        'value' => get_post_meta( $post->ID, 'custom_field', true )
    );
    return $form_fields;
}
add_filter( 'media_post_single_attachment_fields_to_edit', 'custom_attachment_field', 10, 2 );
// Example 2: Restricting the editable fields on the single attachment edit screen
function restrict_attachment_fields( $form_fields ) {
    unset( $form_fields['image-size'] );
    unset( $form_fields['image_alt'] );
    return $form_fields;
}
add_filter( 'media_post_single_attachment_fields_to_edit', 'restrict_attachment_fields' );
// Example 3: Modifying the label of an existing field on the single attachment edit screen
function modify_attachment_field_label( $form_fields ) {
    $form_fields['post_title']['label'] = 'Image Title';
    return $form_fields;
}
add_filter( 'media_post_single_attachment_fields_to_edit', 'modify_attachment_field_label' );