WordPress.org

Forums

[resolved] wp_update_user password change error (2 posts)

  1. jnorion
    Member
    Posted 4 years ago #

    I've created a front-end form which allows logged in users to change their name, email, and password. It uses the wp_update_user function for this purpose.

    I am able to successfully change all these settings using this form. However, when I change the password, even though it changes successfully it gives me a series of error messages on the page:

    Warning: Cannot modify header information - headers already sent by (output started at /www/client/wp-content/themes/modernedge-client/header.php:9) in /www/client/wp-includes/pluggable.php on line 717

    It generates an error for lines 717-734 and 690-692. Looking at the code, these are the lines that clear and then reset cookies, respectively.

    I tried temporarily working around this by commenting out the calls to the cookie functions in the wp_update_user function (which is located in wp-includes/registration.php). This removed the error messages but the password did not update properly either. I'm sure I could have cleared my browser cache manually but I don't want that to be a requirement.

    I read that this error is commonly caused by white space at the beginning or end of the php files, but I couldn't find that in any of the referenced files.

    Here's the code I'm using to accomplish this:

    <?php
    /**
     * Template Name: Profile Settings -- Updated
     *
     */
    
    get_header(); ?>
    
    			<div id="content" class="withSidebar">
    
    				<!-- process POST data -->
    				<?php
    				if ( $_POST['submit-button'] )
    				{
    					include_once(ABSPATH . 'wp-includes/registration.php');
    
    					$post_firstname = $_POST['firstname'];
    					$post_lastname = $_POST['lastname'];
    					$post_email = $_POST['email'];
    					$post_password1 = $_POST['password1'];
    					$post_password2 = $_POST['password2'];
    
    					// pull current user data for comparison
    
    					$current_user = wp_get_current_user();
    					$new_user_data["ID"] = $current_user->ID;
    
    					if ( $post_firstname && $post_firstname !== $current_user->user_firstname )
    						{
    							$new_user_data["first_name"] = $post_firstname;
    						}
    
    					if ( $post_lastname && $post_lastname !== $current_user->user_lastname )
    						{
    							$new_user_data["last_name"] = $post_lastname;
    						}
    
    					if ( $post_email && $post_email !== $current_user->user_email )
    						{
    							$new_user_data["user_email"] = $post_email;
    						}
    
    					if ( $post_password1 && $post_password1 == $post_password2)
    						{
    							$new_user_data["user_pass"] = $post_password1;	
    
    						}
    
    					wp_update_user( $new_user_data );
    					//print_r( $new_user_data );
    				}
    				?>
    
    				<?php $currentUser = wp_get_current_user(); ?>
    				<h1>Account Settings for <?php echo $currentUser->user_firstname . ' ' . $currentUser->user_lastname; ?></h1>
    
                   			<h2>Success!</h2>
    				<p>Your settings have been updated.</p>
    				<p><a href="">Click here</a> to return to the main page, or choose from the menus or sidebar to continue.</p>
    			</div><!-- #content -->
    
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>

    Can anyone tell me what's going wrong with this?

    Thanks!

  2. jnorion
    Member
    Posted 4 years ago #

    Figured it out. Apparently it was a stupid problem on my part.

    wp_update_user only attempts to clear and reset cookies if it's updating the password.

    The php function setcookie(), used in both the cookie-clearing and cookie-resetting functions, adds to the page headers and therefore must be called within the first php tag on the page, and before the WordPress get_header() function. Since wp_update_user needs this, it must be at the beginning of the page as well.

    I was already sending user input to this page via POST anyway, so I just moved all the handling code to the top:

    <?php
    /**
     * Template Name: Profile Settings -- Updated
     *
     */
    
                                    if ( $_POST['submit-button'] )
                                    {
                                            include_once(ABSPATH . 'wp-includes/registration.php');
    
                                            $post_firstname = $_POST['firstname'];
                                            $post_lastname = $_POST['lastname'];
    
    ...... the rest of the code here .....
    
                                            if ( $post_password1 && $post_password1 == $post_password2)
                                                    {
                                                            $new_user_data["user_pass"] = $post_password1;  
    
                                                    }   
    
                                            wp_update_user( $new_user_data );
                                            //print_r( $new_user_data );
                                    }
    get_header(); ?>

Topic Closed

This topic has been closed to new replies.

About this Topic