問題描述

用户個人資料頁面具有以下字段:

用户名稱姓氏暱稱顯示名稱聯繫方式 E-mail 網站 AI​​M Yahoo IM Jabber / Google Talk

如何將更多的字段添加到此部分。字段,如電話號碼,地址或任何其他。

最佳解決方案

您需要使用'show_user_profile''edit_user_profile''personal_options_update''edit_user_profile_update'鈎。

以下是添加電話號碼的代碼:

add_action( 'show_user_profile', 'yoursite_extra_user_profile_fields' );
add_action( 'edit_user_profile', 'yoursite_extra_user_profile_fields' );
function yoursite_extra_user_profile_fields( $user ) {
?>
  <h3><?php _e("Extra profile information", "blank"); ?></h3>
  <table class="form-table">
    <tr>
      <th><label for="phone"><?php _e("Phone"); ?></label></th>
      <td>
        <input type="text" name="phone" id="phone" class="regular-text"
            value="<?php echo esc_attr( get_the_author_meta( 'phone', $user->ID ) ); ?>" /><br />
        <span class="description"><?php _e("Please enter your phone."); ?></span>
    </td>
    </tr>
  </table>
<?php
}

add_action( 'personal_options_update', 'yoursite_save_extra_user_profile_fields' );
add_action( 'edit_user_profile_update', 'yoursite_save_extra_user_profile_fields' );
function yoursite_save_extra_user_profile_fields( $user_id ) {
  $saved = false;
  if ( current_user_can( 'edit_user', $user_id ) ) {
    update_user_meta( $user_id, 'phone', $_POST['phone'] );
    $saved = true;
  }
  return true;
}

該代碼將添加一個字段到您的用户屏幕看起來像這樣:

還有幾個可用於該主題的博客文章可能是有幫助的:

  • 添加和使用自定義用户配置文件字段

  • 向 WordPress 用户配置文件添加額外的字段

或者如果您不想 roll-your-own,則會添加以下功能的插件 (儘管我確信還有其他功能):

  • Cimy 用户額外字段

次佳解決方案

// remove aim, jabber, yim
function hide_profile_fields( $contactmethods ) {
    unset($contactmethods['aim']);
    unset($contactmethods['jabber']);
    unset($contactmethods['yim']);
    return $contactmethods;
}

// add anything else
function my_new_contactmethods( $contactmethods ) {
    //add Birthday
    $contactmethods['birthday'] = 'Birthday';
    //add Address
    $contactmethods['address'] = 'Address';
    //add City
    $contactmethods['city'] = 'City';
    //add State
    $contactmethods['state'] = 'State';
    //add Postcode
    $contactmethods['postcode'] = 'Postcode';
    //add Phone
    $contactmethods['phone'] = 'Phone';
    //add Mobilphone
    $contactmethods['mphone'] = 'Mobilphone';

    return $contactmethods;
}
add_filter('user_contactmethods','my_new_contactmethods',10,1);
add_filter('user_contactmethods','hide_profile_fields',10,1);

希望這可以幫助。

資料來源:WPBeginner

參考文獻

注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。