Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Attachment Fields to Edit Demo
<?php
add_filter( 'attachment_fields_to_edit', 'attachment_location_field', 10, 2 );
add_filter( 'attachment_fields_to_save', 'save_location_field', null, 2 );
function 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;
}
function save_location_field( $post, $attachment ) {
if ( isset( $attachment['location'] ) ) {
update_post_meta( $post['ID'], 'location', sanitize_text_field( $attachment['location'] ) );
} else {
delete_post_meta( $post['ID'], 'location' );
}
return $post;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.