questions

get_user_meta returns first_name as empty

Rate this post

I am trying to access the first_name and last_name of the user in user_register.but it returns empty([“”]).meanwhile in phpadmin in wp_usermeta table I can see that there are a first_name and a last_name.

add_action( 'user_register', 'db_amelia_insert', 10, 2 );
function db_amelia_insert($args){
$userdata = get_user_meta( $args);
echo "<script>alert('".json_encode($userdata)."');</script>";
}

is it possible that another function updated metas after user_register is executed? I am using RegistrationMagic plugin for registration.

 

✔️Solution:

The user_register fires right after a new user gets registered.

So there might be no value in the database when your function runs. But you try can use the value of the input field like:

if ( isset( $_POST['first_name'] ) ) { /* check if there is a value in the input field */
    $first_name = $_POST['first_name']; /* save value of input field in variable */
}

if ( isset( $_POST['last_name'] ) ) { /* check if there is a value in the input field */
    $last_name = $_POST['last_name']; /* save value of input field in variable */
}

and use the variables in your script.

Leave a Reply

Your email address will not be published.

Back to top button