1

I'm trying to update a user mail recieved by $_POST and use it instantly. It gets changed, however it outputs the old one. What is the way to do it?

global $current_user;
get_currentuserinfo();

if (isset($_POST['email'])) {
        wp_update_user( array('ID' => $current_user->ID, 'user_email' => $_POST['email']) );
        get_currentuserinfo(); //just in case
}

echo '<p>You'll recieve a mail here: ' . $current_user->user_email . '</p>'; //outputs old mail

Maybe it has to be with the global $current_user var?

EDIT:

Chuncky fix,

global $current_user;
get_currentuserinfo();

$email = $current_user->user_email;

if (isset($_POST['email'])) {
     wp_update_user( array('ID' => $current_user->ID, 'user_email' => $_POST['email']) );
     $email = $_POST['email'];
}

echo '<p>You'll recieve a mail here: ' . $email . '</p>';
5
  • To update just the email use update_user_meta. Anyway, acording to the docs, get_currentuserinfo() should update the $current_user var. Anyway, if already have the data, you should check if the update was correct, and if it was, use the value in the $_POST
    – Tomás Cot
    Commented Aug 30, 2014 at 2:14
  • It outputs the old database value, I don't know why. Maybe something to be with the cache? The problem in using the $_POST data directly is that it would only be used if it was set. Commented Aug 30, 2014 at 2:23
  • can you change the wp_update_userwith var_dump(wp_update_user( array('ID' => $current_user->ID, 'user_email' => $_POST['email']) )) The idea is to know if the update is working.
    – Tomás Cot
    Commented Aug 30, 2014 at 2:50
  • Yep. It's working. In fact, I see it refreshing Users admin page. Commented Aug 30, 2014 at 2:53
  • I made a provisional fix. I've edited my question. Commented Aug 30, 2014 at 2:57

1 Answer 1

2
    global $current_user;
    get_currentuserinfo();

    if (isset($_POST['email'])) {
            wp_update_user( array('ID' => $current_user->ID, 'user_email' => $_POST['email']) );
            unset($current_user);
            get_currentuserinfo(); //just in case
    }

    echo '<p>You'll recieve a mail here: ' . $current_user->user_email . '</p>'; 

Now it should work, the problem was on the get_currentuserinfo() function, because it checks if the variable is already set and is an instace of WP_User, if it is, the function "aborts" (returns) and doesn't update the variable.

0

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.