0

I've read forum after forum and I feel like my code is perfect. But it still won't update the user email.

     /* Get user info. */
 global $current_user, $wp_roles;
 //get_currentuserinfo(); //deprecated since 3.1

 /* Load the registration file. */
 //require_once( ABSPATH . WPINC . '/registration.php' ); //deprecated since 3.1
 $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_url( $_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['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() );
         exit;
     }
 }

 get_header();
 ?>

    <div id="primary" class="content-area">
        <main id="main" class="site-main">

        <?php
        while ( have_posts() ) :
            the_post(); ?>

      <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
        <header class="entry-header">
            <?php the_title( '<h1 class="entry-title">', '</h1>' ); ?>
        </header><!-- .entry-header -->

        <div class="entry-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 ( 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 -->
                            <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' ) ?>
                                    <input name="action" type="hidden" id="action" value="update-user" />
                            </p><!-- .form-submit -->
                    </form><!-- #adduser -->
            <?php endif; ?>

        </div><!-- .entry-content -->

        <?php if ( get_edit_post_link() ) : ?>
            <footer class="entry-footer">
                <?php
                edit_post_link(
                    sprintf(
                        wp_kses(
                            /* translators: %s: Name of current post. Only visible to screen readers */
                            __( 'Developer Mode <span class="screen-reader-text">%s</span>' ),
                            array(
                                'span' => array(
                                    'class' => array(),
                                ),
                            )
                        ),
                        get_the_title()
                    ),
                    '<span class="edit-link">',
                    '</span>'
                );
                ?>
            </footer><!-- .entry-footer -->
        <?php endif; ?>
      </article><!-- #post-<?php the_ID(); ?> -->

        <?php endwhile; // End of the loop.
        ?>

        </main><!-- #main -->
    </div><!-- #primary -->

If someone could please help me out, please. I can understand most code but I still consider myself a "beginner".

1 Answer 1

0

Your code looks ok.

Maybe this line:

elseif(email_exists(esc_attr( $_POST['email'] )) != $current_user->id )

Should be

elseif(email_exists(esc_attr( $_POST['email'] )) != $current_user->ID )

Every developer should start with learning how to debug code.

Otherwise you're just trying stuff like a headless chicken ;-).


WP Debugging
In your wp-config.php, change:

define( "WP_DEBUG", false );

Into:

define( "WP_DEBUG", true );// just toggle this line to false to turn off
if ( WP_DEBUG ) {
    define( "WP_DEBUG_DISPLAY", false );
    define( "WP_DEBUG_LOG", true );
    @ini_set( "display_errors", 0 );
    define( "SCRIPT_DEBUG", true );
}

Then install a debug log viewer plugin.


When you got the debug log up and running. Add error_log('debug log is working'); directly to your functions.php and check if it's working.

Next, debug your code and check everything.

Example:

/* First check if the email is set correctly */
error_log('email: '.$_POST['email']);

/* Update user information. */
if ( !empty( $_POST['url'] ) )
   wp_update_user( array( 'ID' => $current_user->ID, 'user_url' => esc_url( $_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'] )));
  }
}

/* Check if there's an error */
error_log('error array: <pre>'.print_r($error,true).'</pre>'); 

Check everything untill you find the problem.

You can report back if you found something and you don't know how to fix it.

Regards, Bjorn

1
  • OMG thank you so much for a freakin quick suggestion. I'll test it now and really appreciate it Bjorn!
    – Chanaiya
    Commented Jun 10, 2018 at 16:00

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.