Php 有没有办法在Wordpress注册表中添加下拉列表?

Php 有没有办法在Wordpress注册表中添加下拉列表?,php,wordpress,registration,custom-wordpress-pages,Php,Wordpress,Registration,Custom Wordpress Pages,我正试图在我的Wordpress网站的注册表中添加一个下拉列表。但是我不能解决这个问题(我对PHP非常陌生) 例如: Select - Red - Blue - Green 如果用户选择绿色,则该信息还需要链接到用户配置文件页面。要做到这一点,您需要添加一系列操作,这可能就是您遇到困难的原因。完整代码如下: 编辑:使代码更加干燥 在下拉列表中设置所需的选项 // Set the values we want in dropdown function get_dropdown_options()

我正试图在我的Wordpress网站的注册表中添加一个下拉列表。但是我不能解决这个问题(我对PHP非常陌生)

例如:

Select
- Red
- Blue
- Green

如果用户选择绿色,则该信息还需要链接到用户配置文件页面。

要做到这一点,您需要添加一系列操作,这可能就是您遇到困难的原因。完整代码如下:

编辑:使代码更加干燥

在下拉列表中设置所需的选项

// 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
}
要在前端用户配置文件上显示相同字段,请将其添加到模板文件中:

$user = get_current_user_id()
get_user_meta($user->ID, 'colour_field', false);

谢谢你的帮助!!!这几乎是工作,一切都在那里,除了在管理领域。上面写着“选择一种颜色”,但旁边什么也没有,就好像什么都没有被选中一样。当我有时间在本地开发网站上尝试这一功能时,我会在这之后更新答案,它对我总是有效的…@Jamie刚刚更新了代码并进行了测试,现在完全可以工作了。用这一切来取代我以前的一切。此外,您现在可以使用数组中的第一段代码从一个位置设置下拉列表中的选项。这是一个绝对的图例!今晚我会把代码添加到网站上,但我确信它会很好的工作!!这在我的网站上似乎不起作用。下拉列表显示在表单和后端,但如果我在表单上选择绿色,后端将显示第一个选项(红色)。
/**
 * 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);