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 attachment editor in WordPress
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 ),
        'helps' => 'Enter a custom value for this attachment.'
    );
    return $form_fields;
}
add_filter( 'media_post_single_attachment_fields_to_edit', 'custom_attachment_field', 10, 2 );
// Example 2: Modifying the existing fields in the attachment editor in WordPress
function modify_attachment_fields( $form_fields, $post ) {
    $form_fields['caption']['helps'] = 'Enter a caption for this attachment.';
    return $form_fields;
}
add_filter( 'media_post_single_attachment_fields_to_edit', 'modify_attachment_fields', 10, 2 );
// Example 3: Removing a field from the attachment editor in WordPress
function remove_attachment_field( $form_fields, $post ) {
    unset( $form_fields['description'] );
    return $form_fields;
}
add_filter( 'media_post_single_attachment_fields_to_edit', 'remove_attachment_field', 10, 2 );