我想在我的Wordpress网站的注册表格中添加一个下拉列表。但我无法解决这个问题(我对PHP很陌生)。
举个例子。
Select
- Red
- Blue
- Green
如果用户选择绿色,该信息也需要链接到用户的个人资料页面。
有一系列的操作你需要添加,以做到这一点,这可能是为什么你挣扎。完整的代码如下。
EDIT : 使代码更加DRY
在下拉菜单中设置你想要的选项
// Set the values we want in dropdown
function get_dropdown_options(){
$dropdownOptions = ['Red', 'Green', 'Blue'];
return $dropdownOptions;
}
在前台注册表格中添加字段
add_action( 'register_form', 'unbranded_registration_form' );
function unbranded_registration_form() {
$year = ! empty( $_POST['colour_field'] ) ? intval( $_POST['colour_field'] ) : '';
?>
<p>
<label for="colour_field"><?php esc_html_e( 'Choose a colour', 'unbranded' ) ?><br/>
<select name="colour_field" id="colour_field">
<?php
foreach (get_dropdown_options() as $option) {
echo "<option>$option</option>";
}
?>
</select>
</label>
</p>
<?php
}
决定如何处理任何错误。
add_filter( 'registration_errors', 'unbranded_registration_errors', 10, 3 );
function unbranded_registration_errors( $errors, $sanitized_user_login, $user_email ) {
if ( empty( $_POST['colour_field'] ) ) {
$errors->add( 'colour_field_error', __( '<strong>ERROR</strong>: Please choose a colour.', 'unbranded' ) );
}
return $errors;
}
add_action( 'user_register', 'unbranded_user_register' );
add_action( 'edit_user_created_user', 'unbranded_user_register' );
function unbranded_user_register( $user_id ) {
if ( ! empty( $_POST['colour_field'] ) ) {
update_user_meta( $user_id, 'colour_field', intval( $_POST['colour_field'] ) );
}
}
在网站的管理端添加相同的注册字段。
/**
* Back end registration
*/
add_action( 'user_new_form', 'unbranded_admin_registration_form' );
function unbranded_admin_registration_form( $operation ) {
if ( 'add-new-user' !== $operation ) {
// $operation may also be 'add-existing-user'
return;
}
$year = ! empty( $_POST['colour_field'] ) ? intval( $_POST['colour_field'] ) : '';
?>
<h3><?php esc_html_e( 'Personal Information', 'unbranded' ); ?></h3>
<table class="form-table">
<tr>
<th><label for="colour_field"><?php esc_html_e( 'Choose a colour', 'unbranded' ); ?></label> <span class="description"><?php esc_html_e( '(required)', 'unbranded' ); ?></span></th>
<td>
<select name="colour_field" id="colour_field">
<?php
foreach (get_dropdown_options() as $option) {
echo "<option>$option</option>";
}
?>
</select>
</td>
</tr>
</table>
<?php
}
并以同样的方式处理错误(如果你愿意,你可以重用上面的函数,但把它分开,以防你需要做一些不同的验证
add_action( 'user_profile_update_errors', 'unbranded_user_profile_update_errors', 10, 3 );
function unbranded_user_profile_update_errors( $errors, $update, $user ) {
if ( $update ) {
return;
}
if ( empty( $_POST['colour_field'] ) ) {
$errors->add( 'colour_field_error', __( '<strong>ERROR</strong>: Please choose a colour.', 'unbranded' ) );
}
}
在管理区显示该字段。
/**
* Back end display
*/
// Hooks near the bottom of profile page (if current user)
add_action( 'show_user_profile', 'unbranded_show_extra_profile_fields' );
// Hooks near the bottom of the profile page (if not current user)
add_action( 'edit_user_profile', 'unbranded_show_extra_profile_fields' );
function unbranded_show_extra_profile_fields( $user ) {
?>
<h3><?php esc_html_e( 'Personal Information', 'unbranded' ); ?></h3>
<table class="form-table">
<tr>
<th><label for="colour_field"><?php esc_html_e( 'Choose a colour', 'unbranded' ); ?></label></th>
<td>
<select name="colour_field" id="colour_field">
<?php
$colour = get_the_author_meta( 'colour_field', $user->ID ) ;
foreach (get_dropdown_options() as $option) {
echo "<option ";
if ($colour == $option) {
echo 'selected';
}
echo ">$option</option>";
}
?>
</select>
</td>
</tr>
</table>
<?php
}
允许在管理区更新字段
// Hook is used to save custom fields that have been added to the WordPress profile page (if current user)
add_action( 'personal_options_update', 'update_extra_profile_fields' );
// Hook is used to save custom fields that have been added to the WordPress profile page (if not current user)
add_action( 'edit_user_profile_update', 'update_extra_profile_fields' );
function update_extra_profile_fields( $user_id ) {
if ( current_user_can( 'edit_user', $user_id ) ){
update_user_meta( $user_id, 'colour_field', $_POST['colour_field'] );
}
}
要在前台用户资料上显示相同的字段,请将此添加到你的模板文件中。
$user = get_current_user_id()
get_user_meta($user->ID, 'colour_field', false);