我建议您在类上定义field names属性:
class your_class {
private $_fields = array(
\'firstname\',
\'lastname\',
\'sex\',
);
public function prfx_meta_save ( $post_id ) {
}
}
现在,您可以完全抽象存储处理程序:
foreach ( $this->_fields as $field ) {
if ( ! isset( $_POST[ $field ] ) )
continue;
$value = ( string ) $_POST[ $field ];
$value = wp_unslash( $value ); // WP adds slashes to all request data
$value = sanitize_text_field( $value ); // Fix invalid UTF-8, strip tags & line breaks
update_post_meta( $post_id, $field, $value );
}