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>
if (!empty($item)) {
should beif (!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?