0

I have made a file where I am adding user meta fields but they are only added for a user on update. They are not added when I am creating the user. This is the file:

<?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' );
});

How can I save those meta fields on initial creation of an user, as well?

2

You're missing one more action, that is fired when new user is created:

add_action( 'user_register', 'save_extra_profile_fields', 10, 1 );

documented in wordpress codex.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.