wordpress如何添加多个自定义用户配置文件字段

时间:2015-09-23 08:21:57

标签: wordpress field profile

在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;
}

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

感谢您的回答

2 个答案:

答案 0 :(得分:1)

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

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

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

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

答案 1 :(得分:1)

这是问题中代码片段的更新版本,用于显示多个用户个人资料字段 - 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;
    }