Skip to content

Instantly share code, notes, and snippets.

@teolopez
Last active December 18, 2015 23:28
Show Gist options
  • Save teolopez/5861366 to your computer and use it in GitHub Desktop.
Save teolopez/5861366 to your computer and use it in GitHub Desktop.
Additional contact fields in WordPress, display, and saving additional contact field when registering new Wordpress user. Used bootstrap for registration form part. Add this code to your functions.php
// Add Twitter, Facebook and Google+ field to contact methods, remove AIM, YIM and Jabber
add_filter( 'user_contactmethods', 'ts_add_contact_fields' );
function ts_add_contact_fields( $contactmethods ) {
$contactmethods['linkedin'] = 'LinkedIn';
$contactmethods['twitter'] = 'Twitter';
$contactmethods['facebook'] = 'Facebook';
$contactmethods['googleplus'] = 'Google+';
unset( $contactmethods['yim'] );
unset( $contactmethods['aim'] );
unset( $contactmethods['jabber'] );
return $contactmethods;
}
// Display additional contact fields in WordPress registration form
add_action( 'register_form', 'ts_show_extra_register_fields' );
function ts_show_extra_register_fields() { ?>
<div class="control-group">
<label class="control-label" for="inputEmail">Username</label>
<div class="controls">
<input type="text" name="user_login" placeholder="Username" id="user_login" class="input-medium" />
</div>
</div>
<div class="control-group">
<label class="control-label" for="inputEmail">E-mail</label>
<div class="controls">
<input type="text" name="user_email" placeholder="E-mail" id="user_email" class="input-medium" />
</div>
</div>
<div class="control-group">
<label class="control-label" for="inputEmail">LinkedIn URL</label>
<div class="controls">
<input type="text" name="linkedin" placeholder="LinkedIn" id="linkedin" class="input-medium" />
</div>
</div>
<div class="control-group">
<label class="control-label" for="inputEmail">Twitter</label>
<div class="controls">
<div class="input-prepend">
<span class="add-on">@</span>
<input type="text" name="twitter" placeholder="Twitter" id="twitter" class="input-small" />
</div>
</div>
</div>
<div class="control-group">
<label class="control-label" for="inputEmail">Facebook URL</label>
<div class="controls">
<input type="text" name="facebook" placeholder="Facebook" id="facebook" class="input-medium" />
</div>
</div>
<div class="control-group">
<label class="control-label" for="inputEmail">Google+ URL</label>
<div class="controls">
<input type="text" name="googleplus" placeholder="Google+" id="googleplus" class="input-medium" />
</div>
</div>
<?php }
// Save additional contact fields when registering new WordPress user
add_action( 'user_register', 'ts_register_extra_fields', 100 );
function ts_register_extra_fields( $user_id, $password = '', $meta = array() ) {
$userdata = array();
$userdata['ID'] = $user_id;
$userdata['twitter'] = $_POST['twitter'];
$userdata['facebook'] = $_POST['facebook'];
$userdata['googleplus'] = $_POST['googleplus'];
$new_user_id = wp_update_user( $userdata );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment