3

I use the following to add Phone number,linked id etc Under the "Contact Info" in user profile. Now I have to add a filed under the "About the user". How can i do that?

function my_new_contactmethods( $contactmethods ) {
$contactmethods['designation'] = 'Designation';
$contactmethods['linked_in'] = 'Linked In';
$contactmethods['phone'] = 'Phone Number';
return $contactmethods;
}
add_filter('user_contactmethods','my_new_contactmethods',10,1);

UPDATE

The above code adds 3 fields to under the section "Contact Info " in the user profile I don't want to create a new heading called "About the user" and give the required fields. A section with "About the user " is already there in the profile which includes biographical info and password changing option. I want to add a new field there along with those two

3
  • Have you seen this answer?
    – fuxia
    Apr 19, 2013 at 7:48
  • Sorry but i didnt see any relationship between my question and that question
    – MidhuN
    Apr 19, 2013 at 7:51
  • The answer adds an extra field to the user page that can do much more than the user_contactmethods. There is no hook immediately below the About section.
    – fuxia
    Apr 19, 2013 at 7:56

2 Answers 2

0

Add following code in theme's functions.php file.

add_action( 'show_user_profile', 'extra_user_profile_fields' );
add_action( 'edit_user_profile', 'extra_user_profile_fields' );
function extra_user_profile_fields( $user ) { ?>
 <table class="form-table">
<tr>
<th><label for="address"><?php _e("Designation"); ?></label></th>
<td>
<input type="text" name="designation" id="designation" value="<?php echo esc_attr( get_the_author_meta( 'designation', $user->ID ) ); ?>" class="regular-text" /><br />
</td>
</tr>
<tr>
<th><label for="city"><?php _e("Linked In"); ?></label></th>
<td>
<input type="text" name="linked_in" id="linked_in" value="<?php echo esc_attr( get_the_author_meta( 'linked_in', $user->ID ) ); ?>" class="regular-text" /><br />
</td>
</tr>
<tr>
<th><label for="province"><?php _e("Phone Number"); ?></label></th>
<td>
<input type="text" name="phone" id="phone" value="<?php echo esc_attr( get_the_author_meta( 'phone', $user->ID ) ); ?>" class="regular-text" /><br />
</td>
</tr>
</table>
<?php }
add_action( 'personal_options_update', 'save_extra_user_profile_fields' );
add_action( 'edit_user_profile_update', 'save_extra_user_profile_fields' );
function save_extra_user_profile_fields( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) ) { return false; }
update_user_meta( $user_id, 'designation', $_POST['designation'] );
update_user_meta( $user_id, 'linked_in', $_POST['linked_in'] );
update_user_meta( $user_id, 'phone', $_POST['phone'] );
}
7
  • The code I have given in the question works for me. It adds a field under the "Contact Info". I need to add the same under "About The user" in the user profile.
    – MidhuN
    Apr 19, 2013 at 7:11
  • @MidhuN 'user_contactmethods' filter is used for Customize the contact information fields available to your WordPress users.IF you want separate section for 'about user' then replace you code with the above one.
    – dipali
    Apr 19, 2013 at 7:24
  • I don't want to create a new heading called "About the user" and give the required fields. A section with "About the user " is already there in the profile which includes biographical info and password changing option. I want to add a new field there along with those two
    – MidhuN
    Apr 19, 2013 at 7:30
  • @MidhuN now i updated above code.Please see.I just removed '<h3>' title from the code.
    – dipali
    Apr 19, 2013 at 7:37
  • @MidhuN if it works then let me know
    – dipali
    Apr 19, 2013 at 7:40
0

I have sorted out using the codes above with combination of another code moving phone field. I would like to share these codes with anyone who need them:

/* ADD EXTRA PHONE FIELD INTO USER PROFILE  - WORKING */
// Add a phone field into user profile page
add_action( 'show_user_profile', 'extra_user_profile_fields' );
add_action( 'edit_user_profile', 'extra_user_profile_fields' );
function extra_user_profile_fields( $user ) { ?>
 <table class="form-table">
<tr id="custom_user_field_row">
<th><label for="Phone"><?php _e("Phone Number"); ?></label></th>
<td>
<input type="text" name="phone" id="phone" value="<?php echo esc_attr( 
get_the_author_meta( 'phone', $user->ID ) ); ?>" class="regular-text" /><br />
</td>
</tr>
</table>
<?php }     // Saving Phone field
add_action( 'personal_options_update', 'save_extra_user_profile_fields' );
add_action( 'edit_user_profile_update', 'save_extra_user_profile_fields' );
function save_extra_user_profile_fields( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) ) { return false; }
update_user_meta( $user_id, 'designation', $_POST['designation'] );

update_user_meta( $user_id, 'phone', $_POST['phone'] );
}
function moving_extra_field() { // Moving Phone field above Password on user 
profile page
$screen = get_current_screen();
if ( $screen->id != "profile" && $screen->id != "user-edit" ) 
    return;
?>
<script type="text/javascript">
    jQuery(document).ready(function($) {
        field = $('#custom_user_field_row').remove();
        field.insertBefore('#password');
    });
</script>
<?php
}
add_action( 'admin_head', 'moving_extra_field' );

The above codes will display phone field on user profile page, save it and move it above password. Its not my codes; I combined codes on this site.

Enjoy it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.