You can try the following:
function so_22850878_attachment_fields_to_edit( $form_fields, $post ) { $field_value = get_post_meta( $post->post_parent, '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', 'so_22850878_attachment_fields_to_edit', 10, 2 );
and
/** * Edit attachment fields */ function so_22850878_edit_attachment( $attachment_id ) { $p = get_post( $attachment_id ); if ( isset( $_REQUEST['attachments'][$attachment_id]['location'] ) ) { $location = $_REQUEST['attachments'][$attachment_id]['location']; // Save the value of the 'location' attachment field // to the 'location' post meta of the post parent if it exists: if( ! is_null( $p ) && 0 < $p->post_parent ) update_post_meta( $p->post_parent, 'location', sanitize_text_field( $location ) ); } } add_action( 'edit_attachment', 'so_22850878_edit_attachment' );
to update the meta value of the location column of the parent message from the popup.
You can also check this if you edit the attachment directly from the pages of the media library:
/** * Save attachment fields */ function so_22850878_attachment_fields_to_save( $post, $attachment ) { $p = get_post( $post['ID'] ); // Save the value of the 'location' attachment field // to the 'location' post meta of the post parent if it exists: if( isset( $attachment['location'] ) && ! is_null( $p ) && 0 < $p->post_parent ) update_post_meta( $p->post_parent, 'location', sanitize_text_field( $attachment['location'] ) ); return $post; } add_action( 'attachment_fields_to_save', 'so_22850878_attachment_fields_to_save', 10, 2 );
I'm not sure which workflow you have in mind, but I think there is a problem with your idea, as I understand it:
When you update the location field in the popup menu, it looks like you want to update the meta value of the location post for the parent message. But since the message editing screen does not refresh when you insert images into the message editor, your location value will be overwritten with the old value when updating the message.
So, I wonder if it is possible to use the meta value of a hidden message instead, such as _location ?
Hope this helps.