0

I want to add custom fields, phone and address, in front end of my website.

I've read this question : How do I add a field on the Users profile? For example, country, age etc .

I typed this code inside my function.php

<?php
add_action( 'show_user_profile', 'extra_user_profile_fields' );
add_action( 'edit_user_profile', 'extra_user_profile_fields' );
function extra_user_profile_fields( $user ) { ?>
    <h3><?php _e("Extra profile information", "blank"); ?></h3>

    <table class="form-table">
        <tr>
            <th><label for="address"><?php _e("Address"); ?></label></th>
            <td>
                <input type="text" name="address" id="address" value="<?php echo esc_attr( get_user_meta( $user->ID, 'address', true ) ); ?>" class="regular-text" /><br/>
                <span class="description"><?php _e("Please enter your address."); ?></span>
            </td>
        </tr>
        <tr>
            <th><label for="telephone"><?php _e("Phone number"); ?></label></th>
            <td>
                <input type="tel" name="tel" id="tel" value="<?php echo esc_attr( get_user_meta( $user->ID, 'tel', true ) ); ?>" class="regular-text" /><br />
                <span class="description"><?php _e("Please enter your phone number."); ?></span>
            </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, 'address', $_POST['address'] );
    update_user_meta( $user_id, 'tel', $_POST['tel'] );

}

It's working I can edit and save this fields in my back-office profile page. However when I use the following code (based on the functions) in my page to display and modify thoses informations, address field is working properly but not the phone one.

<?php $user_nz = wp_get_current_user(); ?>
<p>
<span><?php _e( 'Address', ET_DOMAIN ) ?></span><?php echo get_user_meta ($user_nz->ID, 'address', true); ?><!-- affichage adresse -->
</p>
<!-- Affichage téléphdone -->
<p>
<span><?php _e( 'Téléphone', ET_DOMAIN ) ?></span><?php echo get_user_meta ($user_nz->ID, 'tel', true); ?><!-- affichage téléphone -->
</p>

...

<!-- Adresse champs personnalisé caché -->
<div class="fre-input-field">
<label><?php _e( 'Address', ET_DOMAIN ) ?></label>
<input type="text" name="address" id="address" value="<?php echo esc_attr( get_user_meta( $user_nz->ID, 'address', true )); ?>" class="regular-text" /><br/>
</div>
<!-- Adresse champs personnalisé caché -->
<div class="fre-input-field">
<label><?php _e( 'Téléphone', ET_DOMAIN ) ?></label>
<input type="text" name="tel" id="tel" value="<?php echo esc_attr( get_user_meta( $user_nz->ID, 'tel', true )); ?>" class="" /><br/>
</div>

Don't know why address field has a proper behavior and not the phone. By the way I defenied the function am I not supposed to only use do_action('function name'); to do this ?

Thank you

  • What improper behavior is phone showing? Code looks ok when I checked. – Nilambar Jun 13 at 10:31
  • The phone number isn't save and updated. I tried using ACF exact same behavior – DrK Jun 13 at 10:46
  • Instead of <input type="tel", can you try with <input type="text"? – Nilambar Jun 13 at 10:49
  • It has the same behavior – DrK Jun 13 at 10:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.