How to add custom user fields in WordPress

In adding custom user metadata in wordpress is very simple. It can be done by using hooks. This are the following hooks that we’ll use to create a custom user field.
  • show_user_profile
  • edit_user_profile
  • personal_options_update
  • edit_user_profile_update
just copy and paste this sample codes to your functions.php file. “show_user_profile” and “edit_user_profile” action hook is typically used to output new fields or data to the bottom of WordPress’s user profile pages.
                
add_action( ‘show_user_profile’, ‘my_custom_user_fields’ );
add_action( ‘edit_user_profile’, ‘my_custom_user_fields’ );
function my_custom_user_fields( $user ) {
    add_action( ‘show_user_profile’, ‘my_custom_user_fields’ );
add_action( ‘edit_user_profile’, ‘my_custom_user_fields’ );
function my_custom_user_fields( $user ) {
    $user_id = $user->ID;
    $facebook   = get_user_meta( $user_id, ‘facebook’, true );
    $twitter    = get_user_meta( $user_id, ‘twitter’, true );
    $youtube    = get_user_meta( $user_id, ‘youtube’, true );
    $template = ‘
    

Social Media Accounts

Facebook
Please enter your Facebook URL.
Twitter
Please enter your Twitter URL.
Youtube
Please enter your Youtube URL.
‘; echo $template; } }
Note: This action hook is generally used to save custom fields that have been added to the WordPress profile page. “personal_options_update” and “edit_user_profile_update” action hook is generally used to save custom fields that have been added to the WordPress profile page.
                
add_action( ‘personal_options_update’, ‘save_my_custom_usermeta_fields’ );
add_action( ‘edit_user_profile_update’, ‘save_my_custom_usermeta_fields’ );
 
function save_my_custom_usermeta_fields( $user_id ) {
    if ( !current_user_can( ‘edit_user’, $user_id ) ){
        // Do not run the code if current user can’t edit user
        return false;
    }
    update_usermeta( $user_id, ‘facebook’, $_POST[‘facebook’] );
    update_usermeta( $user_id, ‘twitter’, $_POST[‘twitter’] );
    update_usermeta( $user_id, ‘youtube’, $_POST[‘youtube’] );
}