Last active
November 9, 2018 00:07
-
-
Save joemcgill/dbec1854cd02d63b731c583f4a751e7c to your computer and use it in GitHub Desktop.
Attachment Fields to Edit Demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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