WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Refreshing get_currentuserinfo (8 posts)

  1. redrocksrover2
    Member
    Posted 1 year ago #

    Hi,

    I have created a custom user Account Settings page that lets WP users update their account information including additional custom fields that get attached to that user's metadata. One of the fields that can be updated is the user's email address.

    I display the currently logged in user's email address in my header.php file. I use get_currentuserinfo to display the currently logged-in user's email address in the header that is displayed on all pages throughout the site.

    When the user updates the email address of their account by submitting my Account Settings form, the form confirms that the email address is unique, and displays a "Your profile has been updated" message after the successful update (the PHP form evaluates itself, displays errors, and accomplishes the wp_update_user and update_user_meta functions). The problem I'm encountering is that when the form page evaluates itself and successfully updates the user record, the email address in the header.php file isn't updated to reflect the changed email address. I've tried resetting the WP auth cookie at the time that the profile is updated, but that doesn't cause header.php to pick up the new email address. So far the only way I can get the web page (and thus header.php) to display the new email address is to reload the page in the browser.

    I suppose I could have the account settings page redirect to itself after a successful user profile update, but I'm not sure if that's necessary or the best way to accomplish what I'm trying to do.

    Any suggestions for the best way to accomplish what I intend, e.g. that a successful update of the user's email address is reflected in my header.php file after the update?

  2. billibones
    Member
    Posted 1 year ago #

    Could you not write the new value to a SESSION variable and then pass it along that way?
    (ie.

    <?
    session_start;
    if (isset($_SESSION['oldEmail']))
    {
    // user submits form ... new email is written to a variable $newEmail
    // do your update_user_meta etc updates to the DB ...
    // I think session_register is deprecated now
    // query the user table and get your newly updated email address for the ID of the user
    // $resObj-> mysql_fetch_object the new email and register it in a temp session variable.
    
    $_SESSION['newEmail'] = $resObj->user_meta_field_name;
    $newEmail = $_SESSION['newEmail'];
    $oldEmail = $_SESSION['oldEmail'];
    $oldEmail = $newEmail;
    }
    ?>
    ... carry on ...

    Also sessions are specific to the web address in which you are submitting the data and the cookie ID needs to be set the same otherwise you are forced to carry the session over in the URL itself to the next the next location for evaluation: so if you are at http://yourplace.com and your post to http://www.youplace.com you will have problems. Keep the URL exact if it isnt that way already. (ie. <?php echo htmlentities($_SERVER['REQUEST_URI']); ?>)

    This probably a little archaic, and not even certain it will work but just some initial thoughts.

  3. bcworkz
    Member
    Posted 1 year ago #

    I could be wrong, but I believe all WP user settings are cached to minimize DB hits. Part of the solution could involve flushing the cache and forcing it to reload, which is what reloading the page does. Or there's probably a way to update specific cache data. Once the cache is correct, the currentuserinfo function will return current data.

    Not a complete answer, but at least a likely fruitful avenue to follow.

  4. redrocksrover2
    Member
    Posted 1 year ago #

    I'm still working on a solution to my question, and it will likely have to include PHP sessions per billibones. WordPress's internal cache is not persistent from page to page, and the only way to make it so is to use a plugin.

    There are some issues with WordPress and PHP sessions that I'm working around, and once I've ironed out what works, I'll post back. Currently I can set PHP session variables and output them, but regenerating a session with updated variables after submitting a form has been vexing. For some reason WordPress is either holding on to the header data, or I'm not regenerating the session in the right place in my code.

  5. billibones
    Member
    Posted 1 year ago #

    Hey Red, I've got one site that currently uses custom user meta, Im looking through that code to see what I did to navigate this situation and Ill post back ... Hopefully we'll find a resolution.

    Cheers

  6. billibones
    Member
    Posted 1 year ago #

    I think I was heading down the wrong route with looking at a modified plugin of extra_user_details ( i thought maybe serializing the data/unserializing may have had something to do with it but i think I am offbase with that thinking.) ... I did find a topic below that seems to deal with similar headache's with sessions and WP ... maybe it will help.

  7. billibones
    Member
    Posted 1 year ago #

    Hey I did find this out there though that might apply to your situation.

    Adding a function to your template functions.php file:

    function session_manager(){
    	if (!session_id()){
    		session_start();
    		}
    	$_SESSION['email'] = $_POST['email'];
    	}

    Then calling it on the page with your form when the form is posted using add_action

    include 'TEMPLATEPATH . /functions.php';
    add_action('init', 'session_manager');
    .. update user meta etc ... with user inputed data etc ...

    This was from a post here in the forums from 2 years ago regarding a seperate issue with sessions.

  8. redrocksrover2
    Member
    Posted 1 year ago #

    @billibones - Thanks for the pointers.

    I've been able to get this to work - let's just say it's good to pay attention to where the PHP session data is set and regenerated ;-) I had been overwriting my new session data with old variable values. Ahem.

    For others who might be interested:

    1) Out of the box WordPress doesn't respond to PHP sessions without altering the config.php file or the functions.php file. This article was helpful to me: http://devondev.com/2012/02/03/using-the-php-session-in-wordpress/

    2) Once I had set up functions to start PHP sessions and destroy them on login or logout, I was able to manipulate session variables via $_SESSION['key'] in either my header.php file -- or in my particular case where I am processing form data and want the new values submitted by the user to update the session variables -- at the top of my form after the $_POST data has been processed, but before WP's header.php file is output. This way my header.php file picks up the new values of the PHP session variables.

    3) This also gives you the ability to use any of the other PHP session functions such as session_regenerate_id().

Topic Closed

This topic has been closed to new replies.

About this Topic