28

There is default 5 columns named Username Name Email Role Posts in USERS. Now I want to add one more column with his contact number.

How can I achieve this??

Here where black box is shown

  • Do you have phone number setup? I mean can your user add phone numbers in their profile? – Robert hue Sep 6 '14 at 11:08
  • no .. I just want to know how to add .. its not fix that contact number only .. its can b just a blank column also – Rohil_PHPBeginner Sep 6 '14 at 11:09
  • 1
    If your site had a lot of custom columns, you might be interested in a plugin called Admin Columns. – Brian Jun 1 '16 at 19:13
  • you can see this blog with detailed explanation tekina.info/… – Aniket Singh Sep 12 '17 at 17:20
38

Ok, Here is the code to allow your users to add phone numbers. Paste this full code in functions.php file. This will add new field on user profile for "Phone Number" and add a column user table on WordPress admin for phone.

function new_contact_methods( $contactmethods ) {
    $contactmethods['phone'] = 'Phone Number';
    return $contactmethods;
}
add_filter( 'user_contactmethods', 'new_contact_methods', 10, 1 );


function new_modify_user_table( $column ) {
    $column['phone'] = 'Phone';
    return $column;
}
add_filter( 'manage_users_columns', 'new_modify_user_table' );

function new_modify_user_table_row( $val, $column_name, $user_id ) {
    switch ($column_name) {
        case 'phone' :
            return get_the_author_meta( 'phone', $user_id );
        default:
    }
    return $val;
}
add_filter( 'manage_users_custom_column', 'new_modify_user_table_row', 10, 3 );

EDIT

To add two columns you need to make some changes. Compare both codes to understand.

function new_modify_user_table( $column ) {
    $column['phone'] = 'Phone';
    $column['xyz'] = 'XYZ';
    return $column;
}
add_filter( 'manage_users_columns', 'new_modify_user_table' );

function new_modify_user_table_row( $val, $column_name, $user_id ) {
    switch ($column_name) {
        case 'phone' :
            return get_the_author_meta( 'phone', $user_id );
        case 'xyz' :
            return '';
        default:
    }
    return $val;
}
add_filter( 'manage_users_custom_column', 'new_modify_user_table_row', 10, 3 );
  • Use code again. I changed it so you can see phone field on user list too. – Robert hue Sep 6 '14 at 11:20
  • Great! worked .. But at the time of adding user why are they not asking for Phone Number ? – Rohil_PHPBeginner Sep 6 '14 at 11:23
  • Can you please explain this code ? I want to add one more field but it is showing me an error. – Rohil_PHPBeginner Sep 6 '14 at 11:42
  • 2
    I would suggest this be added to a site-specific plugin since it has nothing to do with the theme's appearance. – helgatheviking Sep 6 '14 at 16:57
  • 1
    Finally $user = get_userdata( $user_id ); and return $return are absolutely not necessary. Variable $user is not used by the function and $return is not defined so it is returning nothing. – LoicTheAztec Mar 30 '16 at 10:03

protected by Community Sep 9 '17 at 2:26

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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