Skip to content

Instantly share code, notes, and snippets.

@joemcgill
Last active November 9, 2018 00:07
Show Gist options
  • Save joemcgill/dbec1854cd02d63b731c583f4a751e7c to your computer and use it in GitHub Desktop.
Save joemcgill/dbec1854cd02d63b731c583f4a751e7c to your computer and use it in GitHub Desktop.
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