将代码粘贴到主题函数中。php。我希望它正在写注册表。
功能。php
// Displaying the field
function wp_registration_form() {
$year = ! empty( $_POST[\'year_of_birth\'] ) ? intval( $_POST[\'year_of_birth\'] ) : \'\';
?>
<p>
<label for="year_of_birth"><?php esc_html_e( \'Year of birth\', \'wp\' ) ?><br/>
<input type="number" min="1900" max="2017" step="1" id="year_of_birth" name="year_of_birth" value="<?php echo esc_attr( $year ); ?>"class="input"/>
</label>
</p>
<?php
}
add_action( \'register_form\', \'wp_registration_form\' );
// Validating the field
function wp_registration_errors( $errors, $sanitized_user_login, $user_email ) {
if ( empty( $_POST[\'year_of_birth\'] ) ) {
$errors->add( \'year_of_birth_error\', __( \'<strong>ERROR</strong>: Please enter your year of birth.\', \'wp\' ) );
}
if ( ! empty( $_POST[\'year_of_birth\'] ) && intval( $_POST[\'year_of_birth\'] ) < 1900 ) {
$errors->add( \'year_of_birth_error\', __( \'<strong>ERROR</strong>: You must be born after 1900.\', \'wp\' ) );
}
return $errors;
}
add_filter( \'registration_errors\', \'wp_registration_errors\', 10, 3 );
// Sanitizing and saving the field
function wp_user_register( $user_id ) {
if ( ! empty( $_POST[\'year_of_birth\'] ) ) {
update_user_meta( $user_id, \'year_of_birth\', intval( $_POST[\'year_of_birth\'] ) );
}
}
add_action( \'user_register\', \'wp_user_register\' );
/**
* Back end registration
*/
function wp_admin_registration_form( $operation ) {
if ( \'add-new-user\' !== $operation ) {
// $operation may also be \'add-existing-user\'
return;
}
$year = ! empty( $_POST[\'year_of_birth\'] ) ? intval( $_POST[\'year_of_birth\'] ) : \'\';
?>
<h3><?php esc_html_e( \'Personal Information\', \'wp\' ); ?></h3>
<table class="form-table">
<tr>
<th><label for="year_of_birth"><?php esc_html_e( \'Year of birth\', \'wp\' ); ?></label> <span class="description"><?php esc_html_e( \'(required)\', \'wp\' ); ?></span></th>
<td>
<input type="number"
min="1900"
max="2017"
step="1"
id="year_of_birth"
name="year_of_birth"
value="<?php echo esc_attr( $year ); ?>"
class="regular-text"
/>
</td>
</tr>
</table>
<?php
}
add_action( \'user_new_form\', \'wp_admin_registration_form\' );
// Validating the field
function wp_user_profile_update_errors( $errors, $update, $user ) {
if ( $update ) {
return;
}
if ( empty( $_POST[\'year_of_birth\'] ) ) {
$errors->add( \'year_of_birth_error\', __( \'<strong>ERROR</strong>: Please enter your year of birth.\', \'wp\' ) );
}
if ( ! empty( $_POST[\'year_of_birth\'] ) && intval( $_POST[\'year_of_birth\'] ) < 1900 ) {
$errors->add( \'year_of_birth_error\', __( \'<strong>ERROR</strong>: You must be born after 1900.\', \'wp\' ) );
}
}
add_action( \'user_profile_update_errors\', \'wp_user_profile_update_errors\', 10, 3 );
add_action( \'edit_user_created_user\', \'wp_user_register\' );
// Profile display
function wp_show_extra_profile_fields( $user ) {
?>
<h3><?php esc_html_e( \'Personal Information\', \'wp\' ); ?></h3>
<table class="form-table">
<tr>
<th><label for="year_of_birth"><?php esc_html_e( \'Year of birth\', \'wp\' ); ?></label></th>
<td><?php echo esc_html( get_the_author_meta( \'year_of_birth\', $user->ID ) ); ?></td>
</tr>
</table>
<?php
}
add_action( \'show_user_profile\', \'wp_show_extra_profile_fields\' );
add_action( \'edit_user_profile\', \'wp_show_extra_profile_fields\' );
输出屏幕: