[wp-trac] [WordPress Trac] #40909: Focus in attachment custom fields is lost when updating the value.
    WordPress Trac 
    noreply at wordpress.org
       
    Sun Feb  5 20:38:53 UTC 2023
    
    
  
#40909: Focus in attachment custom fields is lost when updating the value.
-------------------------------------+-------------------------------------
 Reporter:  lucymtc                  |       Owner:  joedolson
     Type:  defect (bug)             |      Status:  accepted
 Priority:  normal                   |   Milestone:  6.2
Component:  Media                    |     Version:  4.7.5
 Severity:  normal                   |  Resolution:
 Keywords:  dev-feedback has-patch   |     Focuses:  accessibility,
  2nd-opinion                        |  javascript
-------------------------------------+-------------------------------------
Comment (by joedolson):
 PHP snippet to add custom fields for testing:
 {{{
 add_filter( 'attachment_fields_to_edit', 'test_fields_40909', 10, 2 );
 function test_fields_40909( $form_fields, $post ) {
         $field_1                = get_post_meta( $post->ID, 'field_1',
 true );
         $form_fields['field_1'] = array(
                 'label' => __( 'Field 1' ),
                 'input' => 'text',
                 'value' => $field_1,
         );
         $field_2                = get_post_meta( $post->ID, 'field_2',
 true );
         $form_fields['field_2'] = array(
                 'label' => __( 'Field 2' ),
                 'input' => 'text',
                 'value' => $field_2,
         );
         return $form_fields;
 }
 add_filter( 'attachment_fields_to_save', 'test_save_fields_40909', 10, 2
 );
 function test_save_fields_40909( $post, $attachment ) {
         if ( isset( $attachment['field_1'] ) ) {
                 update_post_meta( $post['ID'], 'field_1',
 sanitize_text_field( $attachment['field_1'] ) );
         } else {
                 delete_post_meta( $post['ID'], 'field_1' );
         }
         if ( isset( $attachment['field_2'] ) ) {
                 update_post_meta( $post['ID'], 'field_2',
 sanitize_text_field( $attachment['field_2'] ) );
         } else {
                 delete_post_meta( $post['ID'], 'field_2' );
         }
         return $post;
 }
 }}}
-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/40909#comment:13>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
    
    
More information about the wp-trac
mailing list