5

Context

I've created a front-end "edit profile" template. When I input a value into the nickname text box only the nickname is updated. The default WordPress forum has a select menu that allows/requires the user to select their display_name (a text box for the nickname, a select menu for the display_name).

My form doesn't do this because I can't figure out a means of forcing the nickname to update the display_name additionally (i.e. I have a text box for the nickname but not a select menu for the display_name - I want to automate this).

display_name is stored in the 'users' table. nickname is stored in the 'users_meta' table.

Code for saving:

<?php
/* Get user info. */
global $current_user, $wp_roles;
get_currentuserinfo();

/* Load the registration file. */
require_once( ABSPATH . WPINC . '/registration.php' );

/* If profile was saved, update profile. */
if ( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && $_POST['action'] == 'update-user' ) {

    /* Update user password. */
    if ( !empty($_POST['pass1'] ) && !empty( $_POST['pass2'] ) ) {
        if ( $_POST['pass1'] == $_POST['pass2'] )
            wp_update_user( array( 'ID' => $current_user->id, 'user_pass' => esc_attr( $_POST['pass1'] ) ) );
        else
            $error = __('The passwords you entered do not match.  Your password was not updated.', 'profile');
    }

    /* Update user information. */
    if ( !empty( $_POST['url'] ) )
        update_usermeta( $current_user->id, 'user_url', esc_url( $_POST['url'] ) );
    if ( !empty( $_POST['email'] ) )
        update_usermeta( $current_user->id, 'user_email', esc_attr( $_POST['email'] ) );
    if ( !empty( $_POST['first-name'] ) )
        update_usermeta( $current_user->id, 'first_name', esc_attr( $_POST['first-name'] ) );
    if ( !empty( $_POST['last-name'] ) )
        update_usermeta($current_user->id, 'last_name', esc_attr( $_POST['last-name'] ) );
    if ( !empty( $_POST['description'] ) )
        update_usermeta( $current_user->id, 'description', esc_attr( $_POST['description'] ) );




   if ( !empty( $_POST['display_name'] ) )
        update_usermeta($current_user->id, 'display_name', esc_attr( $_POST['display_name'] ) );
   if ( !empty( $_POST['memberlocation'] ) )
        update_usermeta($current_user->id, 'memberlocation', esc_attr( $_POST['memberlocation'] ) );
   if ( !empty( $_POST['skype'] ) )
        update_usermeta($current_user->id, 'skype', esc_attr( $_POST['skype'] ) );



    /* Redirect so the page will show updated info. */
    if ( !$error ) {
        wp_redirect( get_permalink() );
        exit;
    }


global $wpdb;

global $current_user;
$current_user = wp_get_current_user();

$user_id = wp_get_current_user();
$nickname = 'testname';
update_user_meta($user_id, 'nickname', $nickname);


}


?>

Code for form:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
        <div id="post-<?php the_ID(); ?>">
            <div class="entry-content entry">
                <?php the_content(); ?>
                <?php if ( !is_user_logged_in() ) : ?>
                        <p class="warning">
                            <?php _e('You must be logged in to edit your profile.', 'profile'); ?>
                        </p><!-- .warning -->
                <?php else : ?>
                    <?php if ( $error ) echo '<p class="error">' . $error . '</p>'; ?>
                    <form method="post" id="adduser" action="<?php the_permalink(); ?>">




                        <p class="form-nickname">
                            <label for="nickname"><?php _e('Add display name *', 'profile'); ?></label>
                            <input class="text-input" name="nickname" type="text" id="nickname" value="<?php the_author_meta( 'nickname', $current_user->id ); ?>" />
                        </p><!-- .form-memberlocation -->










                        <p class="form-email">
                            <label for="email"><?php _e('E-mail *', 'profile'); ?></label>
                            <input class="text-input" name="email" type="text" id="email" value="<?php the_author_meta( 'user_email', $current_user->id ); ?>" />
                        </p><!-- .form-email -->
                        <p class="form-memberlocation">
                            <label for="memberlocation"><?php _e('Member Location *', 'profile'); ?></label>
                            <input class="text-input" name="memberlocation" type="text" id="memberlocation" value="<?php the_author_meta( 'memberlocation', $current_user->id ); ?>" />
                        </p><!-- .form-memberlocation -->
                        <p class="form-url">
                            <label for="url"><?php _e('Website', 'profile'); ?></label>
                            <input class="text-input" name="url" type="text" id="url" value="<?php the_author_meta( 'user_url', $current_user->id ); ?>" />
                        </p><!-- .form-url -->
                        <p class="form-password">
                            <label for="pass1"><?php _e('Password *', 'profile'); ?> </label>
                            <input class="text-input" name="pass1" type="password" id="pass1" />
                        </p><!-- .form-password -->
                        <p class="form-password">
                            <label for="pass2"><?php _e('Repeat Password *', 'profile'); ?></label>
                            <input class="text-input" name="pass2" type="password" id="pass2" />
                        </p><!-- .form-password -->
                        <p class="form-textarea">
                            <label for="description"><?php _e('Biographical Information', 'profile') ?></label>
                            <textarea name="description" id="description" rows="3" cols="50"><?php the_author_meta( 'description', $current_user->id ); ?></textarea>
                        </p><!-- .form-textarea -->
                        <p class="form-submit">
                            <?php echo $referer; ?>
                            <input name="updateuser" type="submit" id="updateuser" class="submit button" value="<?php _e('Update', 'profile'); ?>" />
                            <?php wp_nonce_field( 'update-user' ) ?>
                            <input name="action" type="hidden" id="action" value="update-user" />
                        </p><!-- .form-submit -->
                    </form><!-- #adduser -->
                    <?php endif; ?>
                </div><!-- .entry-content -->
            </div><!-- .hentry .post -->
            <?php comments_template( '', true ); ?>
            <?php endwhile; ?>
        <?php else: ?>
            <p class="no-data">
                <?php _e('Sorry, no page matched your criteria.', 'profile'); ?>
            </p><!-- .no-data -->
        <?php endif; ?>

My question

How do I force the display_name to be updated whenever the nickname is updated in my custom page template?

1
  • 1
    I don't have time to test your code and respond, just wanted to take a moment and say this is a good example of a well formed question, and as such is worthy of the upvote i gave it..
    – t31os
    Dec 3, 2011 at 14:59

1 Answer 1

1

I'm not 100% sure if I follow the logic of your question. But this is probably what you need:

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

Attention: update_usermeta has been deprecated and you should use update_user_meta.

Also, I really don't know why there is a display_name in both tables, wp_users and wp_users_meta, but I guess this deserves a question of its own.

I believe this lines in your code are for testing or some left over, as I can't see their usefulness and the $current_user/$user_id part is plainly wrong...

global $wpdb;

global $current_user;
$current_user = wp_get_current_user();

$user_id = wp_get_current_user();
$nickname = 'testname';
update_user_meta($user_id, 'nickname', $nickname);

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.