admin管理员组

文章数量:1321070

I'm trying to create my own registration form and adding phone number as required field and I would love to save that phone number as custom user meta.

After validation and santizing inputs, ets. my code looks like this:

// this returns the correct value etc. - string(9) "126777889"
var_dump($metas['user_phone'])

$user_id = wp_insert_user($fields);
add_user_meta( $user_id, 'user_phone', $metas['user_phone'] );

The user is created correctly, but there is something wrong with the phone meta field - in backend it looks like this:

I also tried this approach to test whether the meta exists:

if(get_user_meta($user_id,'user_phone')) {
    update_user_meta($user_id,'user_phone');
} else {
    add_user_meta($user_id,'user_phone');    
}

Also tried to add the meta with 'user_register' action like this:

function addMyCustomMeta($user_id,$meta) {
    update_user_meta($user_id,$meta);
}
add_action('user_register','addMyCustomMeta',10,2,);

And then using like this:

$user_id = wp_insert_user($fields);
do_action('addMyCustomMeta',$user_id,$metas['user_phone']);

In all cases I end up with the situation described in the picture above. No meta title and the value is saved as multiple "Array"

In database it looks fine:

Do you have any idea what am I doing wrong?

Thank you

I'm trying to create my own registration form and adding phone number as required field and I would love to save that phone number as custom user meta.

After validation and santizing inputs, ets. my code looks like this:

// this returns the correct value etc. - string(9) "126777889"
var_dump($metas['user_phone'])

$user_id = wp_insert_user($fields);
add_user_meta( $user_id, 'user_phone', $metas['user_phone'] );

The user is created correctly, but there is something wrong with the phone meta field - in backend it looks like this:

I also tried this approach to test whether the meta exists:

if(get_user_meta($user_id,'user_phone')) {
    update_user_meta($user_id,'user_phone');
} else {
    add_user_meta($user_id,'user_phone');    
}

Also tried to add the meta with 'user_register' action like this:

function addMyCustomMeta($user_id,$meta) {
    update_user_meta($user_id,$meta);
}
add_action('user_register','addMyCustomMeta',10,2,);

And then using like this:

$user_id = wp_insert_user($fields);
do_action('addMyCustomMeta',$user_id,$metas['user_phone']);

In all cases I end up with the situation described in the picture above. No meta title and the value is saved as multiple "Array"

In database it looks fine:

Do you have any idea what am I doing wrong?

Thank you

Share Improve this question edited May 15, 2017 at 9:50 Cafourek asked May 15, 2017 at 9:44 CafourekCafourek 511 gold badge1 silver badge5 bronze badges 2
  • How do you print the values in your HTML code? Sharing your html code would be useful. – Laxmana Commented May 15, 2017 at 9:55
  • Here is complete code: gist.github/cafesk8/78e7ac3555aa94afcb35d815853c4365 HTML only: gist.github/cafesk8/5503fb9c49295c13f8d4c26d3ffcc2e6 – Cafourek Commented May 15, 2017 at 10:12
Add a comment  | 

1 Answer 1

Reset to default 9

You have to trigger the following hooks:

  • user_register
  • personal_options_update
  • edit_user_profile_update

    add_action('user_register', 'addMyCustomMeta');    
    add_action('personal_options_update', 'addMyCustomMeta' );    
    add_action('edit_user_profile_update','addMyCustomMeta' );    
    function addMyCustomMeta( $user_id ) {    
               update_user_meta( $user_id, 'user_phone', $_POST['user_phone'] ); 
    }
    

Hope that helps!!

本文标签: Save custom user meta on registration