Home > OS >  Add a metadata to user
Add a metadata to user

Time:09-23

I'm creating a user and I want to add a metadata "Age" numeric.

The problem is that when I add it, it adds it as string

$user_id = wp_insert_user( array(
    'user_login' => 'janedoe',
    'user_pass' => 'passwordgoeshere',
));
add_user_meta($user_id, 'Age' , 20);

wp_set_current_user($user_id);

var_dump(get_user_meta( get_current_user_id(),"Age"));

This is the result

Array (1) {
   [0] =>
   String (2) "20"
}

Why do not you pick it up as int?

Thanks

CodePudding user response:

add_user_meta( int $user_id, string $meta_key, mixed $meta_value, bool $unique = false )

$meta_value is mixed type to save all variable types, it doesn't know when the value is an integer or a string. So basically, it will convert/sanitize all variables to string.

You can find out more at https://developer.wordpress.org/reference/functions/add_user_meta/

CodePudding user response:

Hyy

Try this,

$user_id = wp_insert_user( array(
    'user_login' => 'janedoe',
    'user_pass' => 'passwordgoeshere',
));
add_user_meta($user_id, 'Age' , 20);

wp_set_current_user($user_id);

$usertest = get_user_meta( get_current_user_id(),"Age", true);
$int = (int)$usertest;
var_dump($int);
  • Related