【问题标题】:wordpress how to add multiple custom user profile fieldwordpress 如何添加多个自定义用户配置文件字段
【发布时间】:2015-12-20 11:13:36
【问题描述】:

在wordpress中 我找到了如何添加自定义用户配置文件字段 1:

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="sample"><?php _e("Sample"); ?></label></th>
      <td>
        <input type="text" name="sample" id="sample" class="regular-text" 
            value="<?php echo esc_attr( get_the_author_meta( 'sample', $user->ID ) ); ?>" /><br />
        <span class="description"><?php _e("Please enter your sample."); ?></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, 'sample', $_POST['sample'] );
    $saved = true;
  }
  return true;
}

但我不知道如何添加多个自定义用户配置文件字段... 有人可以帮助我吗?

感谢您的回答

【问题讨论】:

    标签: wordpress field profile


    【解决方案1】:

    这两个插件可能对你有用。

    https://wordpress.org/plugins/cimy-user-extra-fields/

    https://wordpress.org/plugins/register-plus-redux/

    请尝试以上插件,如有任何疑问,请告诉我。

    【讨论】:

      【解决方案2】:

      这是问题中代码 sn-p 的更新版本,用于显示多个用户配置文件字段 - addresscityregionphone - 代替问题中的 sample

      看一看,试试看是不是你想要的。

          ##################################
          //adding user fields
          ##################################
      
          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="address"><?php _e("Address"); ?></label></th>
                <td>
                  <input type="text" name="address" id="address" class="regular-text" 
                      value="<?php echo esc_attr( get_the_author_meta( 'address', $user->ID ) ); ?>" /><br />
                  <span class="description"><?php _e("Please enter your Address."); ?></span>
              </td>
              </tr>
              <tr>
                <th><label for="city"><?php _e("City"); ?></label></th>
                <td>
                  <input type="text" name="city" id="city" class="regular-text" 
                      value="<?php echo esc_attr( get_the_author_meta( 'city', $user->ID ) ); ?>" /><br />
                  <span class="description"><?php _e("Please enter your City."); ?></span>
              </td>
              </tr>
              <tr>
                <th><label for="region"><?php _e("Region"); ?></label></th>
                <td>
                  <input type="text" name="region" id="region" class="regular-text" 
                      value="<?php echo esc_attr( get_the_author_meta( 'region', $user->ID ) ); ?>" /><br />
                  <span class="description"><?php _e("Please enter your region."); ?></span>
              </td>
              </tr>
              <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, 'address', $_POST['address'] );
              update_user_meta( $user_id, 'city', $_POST['city'] );
              update_user_meta( $user_id, 'region', $_POST['region'] );
              update_user_meta( $user_id, 'phone', $_POST['phone'] );
              $saved = true;
            }
            return true;
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-03
        • 1970-01-01
        • 1970-01-01
        • 2018-09-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多