attachment_fields_to_edit
最后更新于:2021-11-25 20:30:57
apply_filters( ‘attachment_fields_to_edit’, array $form_fields, GC_Post $post )
Filters the attachment fields to edit.
参数
- $form_fields
-
(array)
An array of attachment form fields. - $post
源文件
文件: gc-admin/includes/media.php
// Add custom text/textarea attachment field function add_custom_text_field_to_attachment_fields_to_edit( $form_fields, $post ) { $text_field = get_post_meta($post->ID, 'text_field', true); $form_fields['text_field'] = array( 'label' => 'Custom text field', 'input' => 'text', // you may alos use 'textarea' field 'value' => $text_field, 'helps' => 'This is help text' ); return $form_fields; } add_filter('attachment_fields_to_edit', 'add_custom_text_field_to_attachment_fields_to_edit', null, 2); // Save custom text/textarea attachment field function save_custom_text_attachment_field($post, $attachment) { if( isset($attachment['text_field']) ){ update_post_meta($post['ID'], 'text_field', sanitize_text_field( $attachment['text_field'] ) ); }else{ delete_post_meta($post['ID'], 'text_field' ); } return $post; } add_filter('attachment_fields_to_save', 'save_custom_text_attachment_field', null, 2); // Add custom checkbox attachment field function add_custom_checkbox_field_to_attachment_fields_to_edit( $form_fields, $post ) { $checkbox_field = (bool) get_post_meta($post->ID, 'checkbox_field', true); $form_fields['checkbox_field'] = array( 'label' => 'Checkbox', 'input' => 'html', 'html' => '<input type="checkbox" id="attachments-'.$post->ID.'-checkbox_field" name="attachments['.$post->ID.'][checkbox_field]" value="1"'.($checkbox_field ? ' checked="checked"' : '').' /> ', 'value' => $checkbox_field, 'helps' => '' ); return $form_fields; } add_filter('attachment_fields_to_edit', 'add_custom_checkbox_field_to_attachment_fields_to_edit', null, 2); // Save custom checkbox attachment field function save_custom_checkbox_attachment_field($post, $attachment) { if( isset($attachment['checkbox_field']) ){ update_post_meta($post['ID'], 'checkbox_field', sanitize_text_field( $attachment['checkbox_field'] ) ); }else{ delete_post_meta($post['ID'], 'checkbox_field' ); } return $post; } add_filter('attachment_fields_to_save', 'save_custom_checkbox_attachment_field', null, 2);
add_filter( 'attachment_fields_to_edit', 'my_add_attachment_location_field', 10, 2 ); function my_add_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; } add_action( 'edit_attachment', 'my_save_attachment_location' ); function my_save_attachment_location( $attachment_id ) { if ( isset( $_REQUEST['attachments'][$attachment_id]['location'] ) ) { $location = $_REQUEST['attachments'][$attachment_id]['location']; update_post_meta( $attachment_id, 'location', $location ); } }