WordPress wp_update_user only updates after refresh

I have a custom page in my WordPress installation which, among other things, allows a user to display and change their email address from the front-end.

When they do, though, the email gets correctly changed in the DB, but is not updated on the page until another refresh (with or without a $_POST). This behaviour is really weird and I can’t find the cause.

Read More

Here is the problematic part of code:

if ( !empty( $_POST['user_email'] ) )
    wp_update_user( array ('ID' => $queried_user->id, 'user_email' => esc_attr( $_POST['user_email'] ) ) ) ;

If I execute var_dump($_POST) and var_dump($queried_user), I get that user_email is correctly displayed in the $_POST variable, but, accordingly, remains the old one in the $queried_user variable. Looking at the DB through PHPMyAdmin, though, I can see that the data has already been updated. After another refresh of the browser page, everything displays correctly.

Any pointers?

Related posts

Leave a Reply

1 comment

  1. Not sure how you are populating $queried_user, but try this:

    if ( !empty( $_POST['user_email'] ) )
    {
       $queried_user->user_email=$_POST['user_email'];
       wp_update_user( array ('ID' => $queried_user->id, 'user_email' => esc_attr( $queried_user->user_email ) ) ) ;
    }