Adding Custom Fields to WordPress User Profile and Add New User page

Custom user profile fields in WordPress

Adding custom profile fields in WordPress made easy through action hooks. .Most of the solutions available on internet tell you about adding custom fields on “Edit User” screen, in this tutorial I will tell you how to add your custom fields to Add New User screen

[alert close=”no”]Need help? You can Email me or Skype chat[/alert]

First let’s create a custom field, say “Company Name” and show it on both Add/Update user screens.

function custom_user_profile_fields($user){
    if(is_object($user))
        $company = esc_attr( get_the_author_meta( 'company', $user->ID ) );
    else
        $company = null;
    ?>
    <h3>Extra profile information</h3>
    <table class="form-table">
        <tr>
            <th><label for="company">Company Name</label></th>
            <td>
                <input type="text" class="regular-text" name="company" value="<?php echo $company; ?>" id="company" /><br />
                <span class="description">Where are you?</span>
            </td>
        </tr>
    </table>
<?php
}
add_action( 'show_user_profile', 'custom_user_profile_fields' );
add_action( 'edit_user_profile', 'custom_user_profile_fields' );
add_action( "user_new_form", "custom_user_profile_fields" );

Above code will add a new field labeled “Company Name”. Notice the third hook “user_new_form”, this hook will display the field on Add New User screen.

Lastly we need to save the custom field in database.

function save_custom_user_profile_fields($user_id){
    # again do this only if you can
    if(!current_user_can('manage_options'))
        return false;

    # save my custom field
    update_user_meta($user_id, 'company', $_POST['company']);
}
add_action('user_register', 'save_custom_user_profile_fields');
add_action('profile_update', 'save_custom_user_profile_fields');

Author: Tahir Yasin

Tahir is a Passionate Web Developer from Lahore, Pakistan and a guy with NEVER-STOP-LEARNING attitude who keeps an eye on emerging technologies and customer trends.

17 thoughts on “Adding Custom Fields to WordPress User Profile and Add New User page”

  1. Hi,
    i have tried your solusion but this seems not updating the DB for the new field.

    after i created new user, i go to “edit user” and see the new field is empty.

    what can be the problem?

  2. Hi,
    I’ve got the same problem as foxpc…
    Fields are empty after saving/updating my profile (not tested new user yet).

    Here’s the code I added to the function.php file of my Twenty Fourteen child’s theme:
    /*-----Adding Additionnal User Informations-----*/

    function custom_user_profile_fields($user){
    ?>
    Informations professionelles

    Profession

    <input type="text" class="regular-text" name="job" value="ID ) ); ?>" id="job" />
    IMPORTANT=> si pas de profession, inscrivez: Auteur

    Société ou organisation

    <input type="text" class="regular-text" name="company" value="ID ) ); ?>" id="company" />
    IMPORTANT=> si pas de profession, mettez: Grow Your Dreams

    <?php
    }
    add_action( 'show_user_profile', 'custom_user_profile_fields' );
    add_action( 'edit_user_profile', 'custom_user_profile_fields' );
    add_action( "user_new_form", "custom_user_profile_fields" );

    function save_custom_user_profile_fields($user_id){
    # again do this only if you can
    if(!current_user_can('manage_options'))
    return false;

    # save my custom field
    update_usermeta($user_id, 'job', 'company', $_POST['job'], ['company']);
    }
    add_action('user_register', 'save_custom_user_profile_fields');

  3. Same for me, not saving custom fields when is the user-new.php screen.

    Any ideas?
    Here is my code:

    function tml_edit_user_profile( $profileuser ) { ?>

    Empresa

    <input type="text" class="regular-text" name="empresa" value="empresa; ?>" id="empresa" />
    Informe o nome da empresa.

    <?php
    }
    add_action( 'show_user_profile', 'tml_edit_user_profile' );
    add_action( 'edit_user_profile', 'tml_edit_user_profile' );
    add_action( 'user_new_form', 'tml_edit_user_profile' );

    // SALVA OS DADOS DOS CAMPOS EXTRAS
    function tml_user_register( $user_id ) {
    if ( !empty( $_POST['empresa'] ) )
    update_user_meta( $user_id, 'empresa', $_POST['empresa'] );
    }
    add_action('user_register', 'tml_user_register');
    add_action('profile_update', 'tml_user_register');

    1. Johan,

      I checked your code, it was having some bugs thats why it wasn’t saving data on user creation.

      Here is corrected & tested version of code, hope it will help you.

      /*-----Adding Additionnal User Informations-----*/
      
      function custom_user_profile_fields($user){
      	if(is_object($user))
      	{
              $company = esc_attr( get_the_author_meta( 'company', $user->ID ) );
      		$job = esc_attr( get_the_author_meta( 'job', $user->ID ) );
      	}
          else
      	{
              $company = null;
      		$job = null;
      	}
      ?>
      Informations professionelles
      
      Profession
      
      <input type="text" class="regular-text" name="job" value="<?php echo $job;?>" id="job" />
      IMPORTANT=> si pas de profession, inscrivez: Auteur
      
      Société ou organisation
      
      <input type="text" class="regular-text" name="company" value="<?php echo $company;?>" id="company" />
      IMPORTANT=> si pas de profession, mettez: Grow Your Dreams
      
      <?php
      }
      add_action( 'show_user_profile', 'custom_user_profile_fields' );
      add_action( 'edit_user_profile', 'custom_user_profile_fields' );
      add_action( "user_new_form", "custom_user_profile_fields" );
      
      function save_custom_user_profile_fields($user_id){
      # again do this only if you can
      if(!current_user_can('manage_options'))
      return false;
      
      # save my custom field
      update_user_meta($user_id, 'company', $_POST['company']);
      update_user_meta($user_id, 'job', $_POST['job']);
      }
      add_action('user_register', 'save_custom_user_profile_fields');
      
      1. Hi, Tahir!
        Thanks, man!!

        It didn’t work, maybe is something wrong with my install, I’ve tried a lot of alternatives.

        But, thank you so much for your help!

        Cheers

Leave a Reply

Your email address will not be published. Required fields are marked *