I asked a question yesterday, but I realised that the code was not correct and it was my bad, so I refactored everything. I have a form with two fields which when user submits and takes a quiz test, gets registered and his scores saved to user meta data table. My user gets created, but when I want to add extra meta data of quiz scores for him using update_user_meta it just doesn't do anything. Here is my code:

 if (isset($_POST['submit_contact'])) {
   $email = $_POST['email_contact'];
   $full = $_POST['fullname_contact'];

    $_SESSION["email"] = $email;
    $_SESSION["name"] = $full;
    echo $_SESSION["mail"];

function save_quiz_score() {

     $score = $_POST['score'];

    $email = $_SESSION["email"];
    $username = $_SESSION["name"];
    $user = get_user_by("email", $email);
    register_new_user($email, $_SESSION["email"]);
    update_user_meta($user, "quiz_scores", $score);

    echo $_SESSION["mail"];


I'm using session_start and echo the sessions out and can see the emails, so that is no problem. $_POST['score'] is also valid, because in the past I tried saving it on its own and it worked fine, so neither is that a problem. I just feel like I tried everything and feel like giving up. Maybe an extra pair of eyes can help me and notice a problem. Thanks in advance.

2 Answers 2


You set the variable $user before you created this user..

It should be like this and you need to check if the user already exists

$user = get_user_by("email", $email); // Its return you the user object
if($user) {
    update_user_meta($user->ID, "quiz_scores", $score);
} else {
    $user_id = register_new_user($email, $_SESSION["email"]);
    update_user_meta($user_id, "quiz_scores", $score);

Solved the problem by changing these lines:

register_new_user($email, $_SESSION["email"]);
update_user_meta($user, "quiz_scores", $score);

To this:

$registered = register_new_user($email, $_SESSION["email"]);
    update_user_meta($registered, "quiz_scores", $score);

In the first example, I was trying to get the user from database before actually registering it:

$user = get_user_by("email", $email);

So using $user variable inside update_user_meta() just didn't work.

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.