在创建用户时添加用户元字段

时间:2017-10-31 作者:Leff

我制作了一个文件,在其中添加用户元字段,但它们仅为更新时的用户添加。当我创建用户时,不会添加它们。这是文件:

<?php
add_action(\'init\', function() {
  /**
   * Add new fields above \'Update\' button.
   *
   * @param WP_User $user User object.
   */
  function additional_profile_fields( $user ) {

      $departments = get_terms([\'taxonomy\' => \'department\', \'hide_empty\' => false]);
      $userDepartment = get_the_author_meta( \'department\', $user->ID );
      $regions = get_terms([\'taxonomy\' => \'region\', \'hide_empty\' => false]);
      $userRegion = get_the_author_meta( \'region\', $user->ID );
      $industries = get_terms([\'taxonomy\' => \'industry\', \'hide_empty\' => false]);
      $userIndustry = get_the_author_meta( \'industry\', $user->ID );
      $companies = get_terms([\'taxonomy\' => \'company\', \'hide_empty\' => false]);
      $userCompany = get_the_author_meta( \'company\', $user->ID );

      ?>
      <h3>Extra profile information</h3>
      <table class="form-table">
       <tr>
         <th><label for="department">Avdeling</label></th>
         <td>
           <select id="department" name="department">
             <option value="">Velg alternativ</option>
             <?php
               foreach ( $departments as $department ) {
                 printf( \'<option value="%1$s" %2$s>%1$s</option>\', $department->name, selected( $userDepartment, $department->name, false ) );
               }
             ?>
          </select>
         </td>
       </tr>
      </table>
      <table class="form-table">
       <tr>
         <th><label for="region">Region</label></th>
         <td>
           <select id="region" name="region">
               <option value="">Velg alternativ</option>
               <?php
               foreach ( $regions as $region ) {
                 printf( \'<option value="%1$s" %2$s>%1$s</option>\', $region->name, selected( $userRegion, $region->name, false ) );
               }
             ?>
           </select>
         </td>
       </tr>
      </table>
      <table class="form-table">
       <tr>
         <th><label for="industry">Bransje</label></th>
         <td>
           <select id="industry" name="industry">
              <option value="">Velg alternativ</option>
              <?php
               foreach ( $industries as $industry ) {
                 printf( \'<option value="%1$s" %2$s>%1$s</option>\', $industry->name, selected( $userIndustry, $industry->name, false ) );
               }
             ?>
           </select>
         </td>
       </tr>
      </table>
      <table class="form-table">
       <tr>
         <th><label for="company">Selskap</label></th>
         <td>
           <select id="company" name="company">
             <option value="">Velg alternativ</option>
             <?php
               foreach ( $companies as $company ) {
                 printf( \'<option value="%1$s" %2$s>%1$s</option>\', $company->name, selected( $userCompany, $company->name, false ) );
               }
             ?>
           </select>
         </td>
       </tr>
      </table>
      <?php
  }

  function save_extra_profile_fields( $user_id ) {

    if ( !current_user_can( \'edit_user\', $user_id ) )
        return false;

    update_usermeta( $user_id, \'department\', $_POST[\'department\'] );
    update_usermeta( $user_id, \'region\', $_POST[\'region\'] );
    update_usermeta( $user_id, \'company\', $_POST[\'company\'] );
    update_usermeta( $user_id, \'industry\', $_POST[\'industry\'] );
  }

  add_action(\'user_new_form\', \'additional_profile_fields\');
  add_action(\'edit_user_profile\', \'additional_profile_fields\');
  add_action(\'show_user_profile\', \'additional_profile_fields\');
  add_action( \'personal_options_update\', \'save_extra_profile_fields\' );
  add_action( \'edit_user_profile_update\', \'save_extra_profile_fields\' );
});
如何在最初创建用户时保存这些元字段?

1 个回复
最合适的回答,由SO网友:Levi Dulstein 整理而成

您还缺少一个在创建新用户时激发的操作:

add_action( \'user_register\', \'save_extra_profile_fields\', 10, 1 );
记录于wordpress codex.

结束

相关推荐