这可以通过两个过滤器完成,attachment_fields_to_edit
和attachment_fields_to_save
, 这完全符合他们的名字。假设您想添加source
领域首先,生成字段并用其当前值(如果存在)填充它:
function wpse133608_add_attachment_location_field( $form_fields, $post ) {
$field_value = get_post_meta( $post->ID, \'source\', true );
$form_fields[\'source\'] = array(
\'value\' => $field_value ? $field_value : \'\',
\'label\' => __( \'Source\' ),
\'helps\' => __( \'Set a source for this attachment\' )
);
return $form_fields;
}
add_filter( \'attachment_fields_to_edit\', \'wpse133608_add_attachment_location_field\', 10, 2 );
然后,如果该字段包含以下内容,请确保该字段已保存:
function wpse133608add_image_attachment_fields_to_save( $post, $attachment ) {
if ( isset( $attachment[\'source\'] ) )
update_post_meta( $post[\'ID\'], \'_source\', esc_attr($attachment[\'source\']) );
return $post;
}
add_filter("attachment_fields_to_save", "wpse133608_add_image_attachment_fields_to_save", null , 2);
阅读更多信息
this extensive tutorial.