I wanted to add additional fields / options to the WordPress User Profiles, this would enable users to add their Twitter, Facebook, and Phone Number. Below is a snippet of code you can either add to functions.php or integrate into your WordPress plugin!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php // Personal Options | |
add_action( 'personal_options_update', 'save_custom_profile_fields' ); | |
add_action( 'edit_user_profile_update', 'save_custom_profile_fields' ); | |
function save_custom_profile_fields( $user_id ) { | |
update_user_meta( $user_id, 'phone_number', $_POST['phone_number'], get_user_meta( $user_id, 'phone_number', true ) ); | |
update_user_meta( $user_id, 'greeting', $_POST['greeting'], get_user_meta( $user_id, 'greeting', true ) ); | |
} | |
add_filter( 'user_contactmethods', 'add_contact_option', 10, 2 ); | |
function add_contact_option( $user_contactmethods, $user ) { | |
$user_contactmethods['phone_number'] = 'Phone Number'; | |
return $user_contactmethods; | |
} | |
add_action( 'personal_options', 'add_profile_options'); | |
function add_profile_options( $profileuser ) { | |
$greeting = get_user_meta($profileuser->ID, 'greeting', true); | |
?><tr> | |
<th scope="row">Greeting</th> | |
<td><input type="text" name="greeting" value="<?php echo $greeting; ?>" /></td> | |
</tr><?php | |
} |
Adding A Drop Down Menu To personal_options
If your curious how to add a select drop down menu, below is an example on how to do this. I hope you find this useful!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php // Personal Options | |
add_action( 'personal_options_update', 'save_custom_profile_fields' ); | |
add_action( 'edit_user_profile_update', 'save_custom_profile_fields' ); | |
function save_custom_profile_fields( $user_id ) { | |
update_user_meta( $user_id, 'teampage', $_POST['teampage'], get_user_meta( $user_id, 'teampage', true ) ); | |
} | |
add_action( 'personal_options', 'add_profile_options'); | |
function add_profile_options( $profileuser ) { | |
$greeting = get_user_meta($profileuser->ID, 'teampage', true); | |
?><tr> | |
<th scope="row">Include On Meet The Team Page?</th> | |
<td> | |
<select name="teampage" id="teampage" > | |
<option id="Yes"<?php selected( $profileuser->teampage, 'Yes' ); ?>>Yes</option> | |
<option id="No"<?php selected( $profileuser->teampage, 'No' ); ?>>No</option> | |
</select> | |
</td> | |
</tr><?php | |
} |
How To Remove Default personal_options in User Profiles
Below is a snippet of code that allows you to remove personal_options in the WordPress User Profile.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Remove Default personal_options | |
add_filter('user_contactmethods','hide_profile_fields',10,1); | |
function hide_profile_fields( $contactmethods ) { | |
unset($contactmethods['aim']); | |
unset($contactmethods['jabber']); | |
unset($contactmethods['yim']); | |
return $contactmethods; | |
} |
Now How Do I Echo This Onto A Page!?
I am assuming you know a little bit about PHP, WordPress Theme development and the API. Below is a function you can use. If you need more info on the_author_meta() and get_the_author_meta() functions please visit the codex. Please feel free to ask a question in comments.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
the_author_meta('jobtitle', $authorID); | |
get_the_author_meta('teampage', $authorID); |