Loading...

Add Xtra Profile Information !

faisal / Wordpress

By default, WordPress has limited number of profile information available for users. Say for example you would like users to register to your site but also include their phone number while registering instead of just email address. How to do that ?!

Well, its super easy !

Open your functions.php file or use any file on as a plugin if you like. Place these codes

add_action(‘show_user_profile’,’show_extra_inforamtion’);
add_action(‘edit_user_profile’,’show_extra_inforamtion’);
function show_extra_inforamtion($user){?>
    <h3><?php _e(‘Extra profile info’,’blank’)?></h3>
    <table class=’form-table’>
        <tr>
            <th><label for=”phone”><?php _e(‘Phone’,’blank’); ?></label></th>
            <td>
                <input type=”text” id=”phone” name=”phone” value=”<?php echo esc_attr(get_the_author_meta(‘phone’,$user->ID)); ?>”/>
                <br/><span><?php _e(‘please enter phone’,’blank’); ?></span>
            </td>        
        </tr> 

</table>
<?php }
add_action(‘edit_user_profile_update’,’save_extra_infromation’);
add_action(‘personal_options_update’,’save_extra_infromation’);
function save_extra_infromation($user_id){
    if(!current_user_can(‘edit_user’,$user_id)){ return false;}
    update_usermeta($user_id,’phone’,$_POST[‘phone’]);
}
?>

 

Now checkout your ADMIN-USERS-YOURPROFILE   page from Dashboard. You will see an extra field of PHONE.

Here you can update the information for user. But wait a minute ! this still doesn’t show up a PHONE extra row when registering new user right?  So, here’s what we do:

<?php

add_action(‘register_post’,’add_register_field_validate_phone’,10,3);
function add_register_field_validate_phone($sanitized_user_login,$user_email,$errors){
    if(!isset($_POST[‘phone’]) || empty($_POST[‘phone’])){
        return $errors->add( ‘lastnameempty’, ‘<strong>ERROR</strong>: Please provide phone.’ );

    }

}
add_action( ‘user_register’, ‘insert_register_fields’ );
function insert_register_fields( $user_id ) {

    $phone = apply_filters(‘pre_user_phone’,$_POST[‘phone’]);

    update_user_meta($user_id,’phone’,$phone);
}

?>

 

Now we are done!

You may add more information in similar way!