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
Need help? You can Email me or Skype chat
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');
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?
share your code here
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');
Sorry code is not showing properly… but you got the idea 😉
use below hook for saving data on profile update
add_action(‘profile_update’, ‘save_custom_user_profile_fields’);
Thanks for your answer,
I finnally did the job with ACF plugin (as I already use it).
But this will help other readers of this tutorial.
Hi,
I want save these fields in to database under $wpdb->users.
can u help me?
I’m having this same problem. Shows the fields for new users but doesn’t save the information.
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');
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.
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
I click “Update” – not saving fields
where can I add this code , I just don’t know where to add it
You can add it in functions.php of your theme or create a new plugin and add this code there.
Hi ,Tahir
how can i get this value of filed , with get_user_meta() Function?
thanks in advance
Hi again
i did it.
thanks
Hi ,
I also tried this code .
But i want to do this through a plugin .Please tell mw how can i do this ?