How to change the display_name by adding a custom field to the user?

Ola,

Is there any way to change the display_name progammatically to add a extra field. So, for example, I currently use "Firstname Lastname" but I want to add a field "Title".

Thanks in advance!

  • Ash

    Hello @Inesta

    I hope you are well today and thanks for asking the question.

    You can add extra field, yes, but you can't replace display name with that. You have to code yourself and use that custom code in everywhere where you want to show custom field value.

    This is an example of adding a field for facebook url:

    add_action( 'show_user_profile', 'extra_fields' );
    add_action( 'edit_user_profile', 'extra_fields' );
    add_action( 'personal_options_update', 'save_fields' );
    add_action( 'edit_user_profile_update', 'save_fields' );
    
    function extra_fields($user) {
    ?>
    <h3><?php _e("Extra profile information", "DOMAIN"); /*DOMAIN = Lang domain for l10n (optional)*/ ?></h3>
    <table class="form-table">
    <tbody>
    <tr>
    <th><?php _e("Extra profile information", "DOMAIN"); /*DOMAIN = Lang domain for l10n (optional)*/ ?></th>
    <td><input class="regular-text" id="facebook" type="text" name="facebook" value="<?php echo esc_attr( get_the_author_meta( 'facebook', $user->ID ) ); ?>" /></td>
    </tr>
    </tbody>
    </table>
    <?php }
    
    function save_fields( $user_id ) {
    if ( !current_user_can( 'edit_user', $user_id ) ) { return false; } //Checking if the current user has ability to edit the user profile information
    update_user_meta( $user_id, 'facebook', $_POST['facebook'] );
    }

    Hope it helps :slight_smile: Please feel free to ask more question if you have.

    Cheers
    Ash

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.