Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 36 You must be signed in to star a gist
  • Fork 14 You must be signed in to fork a gist
  • Save chrisdigital/5525127 to your computer and use it in GitHub Desktop.
Save chrisdigital/5525127 to your computer and use it in GitHub Desktop.
Setting up a editable user profile in WordPress on the frontend.
//How to edit a user profile on the front end?
//http://wordpress.stackexchange.com/questions/9775/how-to-edit-a-user-profile-on-the-front-end
//Forcing nickname as display_name in custom edit profile template
//http://wordpress.stackexchange.com/questions/35403/forcing-nickname-as-display-name-in-custom-edit-profile-template
///////
<?php
/**
* Template Name: User Profile
*
* Allow users to update their profiles from Frontend.
*
/* Get user info. */
global $current_user, $wp_roles;
get_currentuserinfo();
/* Load the registration file. */
require_once( ABSPATH . WPINC . '/registration.php' );
$error = array();
/* 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'] ) )
wp_update_user( array ('ID' => $current_user->ID, 'user_url' => esc_attr( $_POST['url'] )));
if ( !empty( $_POST['email'] ) ){
if (!is_email(esc_attr( $_POST['email'] )))
$error[] = __('The Email you entered is not valid. please try again.', 'profile');
elseif(email_exists(esc_attr( $_POST['email'] )) != $current_user->id )
$error[] = __('This email is already used by another user. try a different one.', 'profile');
else{
wp_update_user( array ('ID' => $current_user->ID, 'user_email' => esc_attr( $_POST['email'] )));
}
}
if ( !empty( $_POST['first-name'] ) )
update_user_meta( $current_user->ID, 'first_name', esc_attr( $_POST['first-name'] ) );
if ( !empty( $_POST['last-name'] ) )
update_user_meta($current_user->ID, 'last_name', esc_attr( $_POST['last-name'] ) );
if ( !empty( $_POST['display_name'] ) )
wp_update_user(array('ID' => $current_user->ID, 'display_name' => esc_attr( $_POST['display_name'] )));
update_user_meta($current_user->ID, 'display_name' , esc_attr( $_POST['display_name'] ));
if ( !empty( $_POST['description'] ) )
update_user_meta( $current_user->ID, 'description', esc_attr( $_POST['description'] ) );
/* Redirect so the page will show updated info.*/
/*I am not Author of this Code- i dont know why but it worked for me after changing below line to if ( count($error) == 0 ){ */
if ( count($error) == 0 ) {
//action hook for plugins and extra fields saving
do_action('edit_user_profile_update', $current_user->ID);
wp_redirect( get_permalink().'?updated=true' ); exit;
}
}
get_header(); // Loads the header.php template. ?>
<?php do_atomic( 'before_content' ); // florence_before_content ?>
<section id="content">
<?php do_atomic( 'open_content' ); // florence_open_content ?>
<?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 : ?>
<h3>Update Information for &quot;<?php echo $current_user->user_login ?>&quot;</h3></br>
<?php if ( $_GET['updated'] == 'true' ) : ?> <div id="message" class="updated"><p>Your profile has been updated.</p></div> <?php endif; ?>
<?php if ( count($error) > 0 ) echo '<p class="error">' . implode("<br />", $error) . '</p>'; ?>
<form method="post" id="adduser" action="<?php the_permalink(); ?>">
<p class="form-username">
<label for="first-name"><?php _e('First Name', 'profile'); ?></label>
<input class="text-input" name="first-name" type="text" id="first-name" value="<?php the_author_meta( 'first_name', $current_user->ID ); ?>" />
</p><!-- .form-username -->
<p class="form-username">
<label for="last-name"><?php _e('Last Name', 'profile'); ?></label>
<input class="text-input" name="last-name" type="text" id="last-name" value="<?php the_author_meta( 'last_name', $current_user->ID ); ?>" />
</p><!-- .form-username -->
<!-- .form-display_name -->
<p class="form-display_name"><label for="display_name"><?php _e('Display name publicly as') ?></label>
<select name="display_name" id="display_name"><br/>
<?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><!-- .form-display_name -->
<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-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 -->
<?php
//action hook for plugin and extra fields
do_action('edit_user_profile',$current_user);
?>
<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_'. $current_user->ID ) ?>
<input name="action" type="hidden" id="action" value="update-user" />
</p><!-- .form-submit -->
</form><!-- #adduser -->
<?php endif; ?>
</div><!-- .entry-content -->
</div><!-- .hentry .post -->
<?php endwhile; ?>
<?php else: ?>
<p class="no-data">
<?php _e('Sorry, no page matched your criteria.', 'profile'); ?>
</p><!-- .no-data -->
<?php endif; ?>
<?php do_atomic( 'close_content' ); // florence_close_content ?>
</section><!-- #content -->
<?php do_atomic( 'after_content' ); // florence_after_content ?>
<?php get_footer(); // Loads the footer.php template. ?>
@milapdave
Copy link

Hi,
i use same code for user field but i want to add multiple image in user profile.
how can i add multiple image in user profile ?

@chrisdigital
Copy link
Author

Yes, I don't see why not. You have complete control of the code.

@emanueless
Copy link

Tnx, I have additional fields on each user but this hook loads everythings ok but wrong attidional fields.
Loads additional fields of other user

@congomonster
Copy link

Hi,
need some help with this. I can't update the e-mail adress. It says that the e-mail adress is in use. But there are only two users.
Any idea whats wrong?

@congomonster
Copy link

@benbinesh
Copy link

Updated Line No 39 to 47 with code below in order to work for updating the new e-mail address

$email_exits = email_exists(esc_attr($_POST['email'])); 
 if ( !empty( $_POST['email'] ) ){
        if (!is_email(esc_attr( $_POST['email'] )))
            $error[] = __('The Email you entered is not valid.  please try again.', 'profile');
       elseif ($email_exits && $email_exits != $current_user->id)
            $error[] = __('This email is already used by another user.  try a different one.', 'profile');
        else{
            wp_update_user( array ('ID' => $current_user->ID, 'user_email' => esc_attr( $_POST['email'] )));
        }
    }

Copy link

ghost commented Nov 15, 2017

Hello
Thank you for the template! Once I add it to the website and use it for the desired page I do not see any content displayed. What could be the reason?

@vladutilie
Copy link

Hello,
Can you post all the code from your file and tell us the environment that you use?

@nitinver12
Copy link

how to update profile image

@westzonesolution1
Copy link

westzonesolution1 commented Jul 17, 2021

It does not work for me. So I have managed to modify the code. Works fine for me. Here I am updating any specific user profile, you can edit the current user also. Just define the current user Id into $user_id:

<?php
require_once('../../../wp-load.php');

/* Load the registration file. */
require_once( ABSPATH . WPINC . '/registration.php' );
$error = array();    

function update_basic_user_meta() {     //define this function    

$user_id = $_POST['user_id'];
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email = $_POST['user_email'];

/* Extra ACF Fileds */
$company = $_POST['company'];

    $email_exits = email_exists(esc_attr($_POST['user_email'])); 
    
    if ( !empty( $_POST['user_email'] ) ){
        if (!is_email(esc_attr( $_POST['user_email'] )))
            $error[] = __('The Email you entered is not valid.  please try again.', 'profile');
        elseif ($email_exits && $email_exits != $user_id)
            $error[] = __('This email is already used by another user.  try a different one.', 'profile');
        else{
            wp_update_user( array ('ID' => $user_id, 'user_email' => esc_attr( $_POST['user_email'] )));
        }
    }
    
    if ( !empty( $_POST['first_name'] ) )
        update_user_meta( $user_id, 'first_name', esc_attr( $_POST['first_name'] ) );
    if ( !empty( $_POST['last_name'] ) )
        update_user_meta($user_id, 'last_name', esc_attr( $_POST['last_name'] ) );

        
    /* Upate ACF Fields */

    if ( ! empty( $_POST['company'] ) ) {
        update_field('company', $_POST['company'], 'user_'.$user_id);
    }
    
    
    if ( is_wp_error( $user_data ) ) {
        // There was an error; possibly this user doesn't exist.
        echo '<span class="failure alert_pop">We found some techcnial glitch. Contact with your software provider.</span>.';
    } else {
        // Success!
        echo '<span class="sucess alert_pop">Account details of '.$first_name.' '.$first_name.' successfully updated.</span>' ;
    }

}  //end the function

update_basic_user_meta();   //call to the function
add_action('init', 'update_basic_user_meta'); // don't know is this necessary after calling the function but still add action of this function.

?>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment