0

How can i add user display name drop down menu in frontend? then user can choose preferred display name?

In fact, I used this code answered here: How to edit a user profile on the front end?

submit handler:

if (!empty($_POST['display_name'])) {
    update_user_meta($current_user->ID, 'display_name', esc_attr($_POST['display_name']));
}

and form:

<p class="form-display-name">
<label for="display-name"><?php _e('display-name', 'profile');?></label>
            <select name="display_name" id="display_name">
        <?php
            $public_display                     = array();
            $public_display['display_nickname'] = $current_user->nickname;
            $public_display['display_username'] = $current_user->user_login;

        if ( ! empty( $current_user->first_name ) ) {
            $public_display['display_firstname'] = $current_user->first_name;
        }

        if ( ! empty( $current_user->last_name ) ) {
            $public_display['display_lastname'] = $current_user->last_name;
        }

        if ( ! empty( $current_user->first_name ) && ! empty( $current_user->last_name ) ) {
            $public_display['display_firstlast'] = $current_user->first_name . ' ' . $current_user->last_name;
            $public_display['display_lastfirst'] = $current_user->last_name . ' ' . $current_user->first_name;
        }

        if ( ! in_array( $current_user->display_name, $public_display ) ) { // Only add this if it isn't duplicated elsewhere
            $public_display = array( 'display_displayname' => $current_user->display_name ) + $public_display;
        }

            $public_display = array_map( 'trim', $public_display );
            $public_display = array_unique( $public_display );

        foreach ( $public_display as $id => $item ) {
            ?>
        <option <?php selected( $current_user->display_name, $item ); ?>><?php echo $item; ?></option>
            <?php
        }
        ?>
        </select>
</p>
8
  • What do you need: help with the HTML submitting it, or script if you want it to save without hitting a 'submit' button, or writing an endpoint to accept the submission (a whole page? admin-ajax? REST API?), or how to then save the value into the user meta?
    – Rup
    Feb 4, 2020 at 12:01
  • Please look at my code. question updated
    – Navid M
    Feb 4, 2020 at 16:41
  • 1
    There is no question mark in the sourcecode. Please write a simple sentence formulating a question. Feb 4, 2020 at 16:50
  • In your submit handler I'd guess if (!empty($item)) { should be if (!empty($_POST['display_name'])) { since there's no $item defined. But I'd expect you'd get a warning or error about that at least. Do you get any errors? Can you show us them?
    – Rup
    Feb 4, 2020 at 17:02
  • @Rup : I used your solution. doesnt work and it don`t show any errors. Profile updated but no change in display name
    – Navid M
    Feb 5, 2020 at 17:12

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.