我所做的是用字段创建表单并验证它们:添加这行代码以添加用户对必填字段的了解
<span class="description"><?php _e(\'(required)\'); ?></span>
将此添加到如下表单:
add_action( \'show_user_profile\', \'extra_user_profile_fields\' );
add_action( \'edit_user_profile\', \'extra_user_profile_fields\' );
function extra_user_profile_fields( $user ) { ?>
<table class="form-table">
<tr>
<th><label for="address"><?php _e("address",\'shabatkeeper\'); ?> <span class="description"><?php _e(\'(required)\'); ?></span></label></th>
<td>
<input type="text" name="address" id="address" value="<?php echo esc_attr( get_the_author_meta( \'address\', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e("Please enter your address.",\'shabatkeeper\'); ?> </span>
</td>
</tr>
等等。。
然后添加了一个validate if is empty and if empty do error消息:
add_action( \'personal_options_update\', \'save_extra_user_profile_fields\' );
add_action( \'edit_user_profile_update\', \'save_extra_user_profile_fields\' );
function save_extra_user_profile_fields( $user_id ) {
if ( !current_user_can( \'edit_user\', $user_id ) ) { return false; }
{
if (!isset($_POST[\'address\']) || empty($_POST[\'address\']) {
wp_die( __(\'Error: you must fill all fields\') );
} else {
// field was set and contains a value
// do your action
exit;
}
}
}
也许还有另一条捷径,所以如果你知道,请告诉我。