How To Add Options To User Profiles Using personal_options

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!


<?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-&gt;ID, 'greeting', true);
?&gt;&lt;tr&gt;
&lt;th scope=&quot;row&quot;&gt;Greeting&lt;/th&gt;
&lt;td&gt;&lt;input type=&quot;text&quot; name=&quot;greeting&quot; value=&quot;&lt;?php echo $greeting; ?&gt;&quot; /&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;?php
}

view raw

gistfile1.txt

hosted with ❤ by GitHub

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!


<?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-&gt;ID, 'teampage', true);
?&gt;&lt;tr&gt;
&lt;th scope=&quot;row&quot;&gt;Include On Meet The Team Page?&lt;/th&gt;
&lt;td&gt;
&lt;select name=&quot;teampage&quot; id=&quot;teampage&quot; &gt;
&lt;option id=&quot;Yes&quot;&lt;?php selected( $profileuser-&gt;teampage, 'Yes' ); ?&gt;&gt;Yes&lt;/option&gt;
&lt;option id=&quot;No&quot;&lt;?php selected( $profileuser-&gt;teampage, 'No' ); ?&gt;&gt;No&lt;/option&gt;
&lt;/select&gt;
&lt;/td&gt;
&lt;/tr&gt;&lt;?php
}

view raw

gistfile1.txt

hosted with ❤ by GitHub

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.


// 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;
}

view raw

gistfile1.txt

hosted with ❤ by GitHub

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.


the_author_meta('jobtitle', $authorID);
get_the_author_meta('teampage', $authorID);

view raw

gistfile1.txt

hosted with ❤ by GitHub

Leave a Reply

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

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax