0

I am developing my plugin to add a custom attachment fields. I used attachment_fields_to_edit filter (https://codex.wordpress.org/Plugin_API/Filter_Reference/attachment_fields_to_edit) to resolve my mission. However, when I go the attachment form page, it shows a field "Required fields are marked *". You can have a look at below picture.

Screenshot

Edit: added my soure code

function my_add_attachment_location_field( $form_fields, $post ) {
    $field_value = get_post_meta( $post->ID, 'location', true );
    $form_fields['location'] = array(
        'value' => $field_value ? $field_value : '',
        'label' => __( 'Location' ),
        'helps' => __( 'Set a location for this attachment' )
    );
    return $form_fields;
}
add_filter( 'attachment_fields_to_edit', 'my_add_attachment_location_field', 10, 2 );

function my_save_attachment_location( $attachment_id ) {
    if ( isset( $_REQUEST['attachments'][$attachment_id]['location'] ) ) {
        $location = $_REQUEST['attachments'][$attachment_id]['location'];
        update_post_meta( $attachment_id, 'location', $location );
    }
}
add_action( 'edit_attachment', 'my_save_attachment_location' );
4
  • All soothsayers are on vacation now. Please show us your code. – Max Yudin Jan 18 '18 at 6:46
  • hi @MaxYudin, I am using exactly the example code in the link codex.wordpress.org/Plugin_API/Filter_Reference/… – gaupoit Jan 18 '18 at 7:04
  • 1
    This line is hardcoded in the wp-admin/includes/media.php with no check of the presence of the required fields. Looks like nobody recognize it as issue (#1746). I see two ways to get rid of this line: preg_replace() and JavaScript. The latter seems simpler. – Max Yudin Jan 18 '18 at 7:44
  • Thanks for your tips @MaxYudin, I will try to implement it. – gaupoit Jan 18 '18 at 13:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.