-1

I'm trying to add a few custom field to the registration form using the code below. After registrations customs fields with keys are created but they don't have any value. I think that my $_POST['user_register_vardas'] doesn't carry any value inside.

my html field look like that:

<input type="text" name="user_register_vardas" id="user_register_vardas" placeholder="<?php esc_attr_e('Vardas','urbango-membership') ?>" value="" required />

and my function:

add_action('user_register', 'register_extra_fields');
function register_extra_fields ( $user_id, $password = "", $meta = array() )
{
    update_user_meta( $user_id, 'vardas', $_POST['user_register_vardas'],true);
    update_user_meta( $user_id, 'pavarde', $_POST['user_register_pavarde'], false );
    update_user_meta( $user_id, 'telefonas', $_POST['user_register_vardas'] );
    update_user_meta( $user_id, $_POST['user_register_vardas'], $_POST['user_register_vardas'] ); //this key is not created
}
0

There are a number of issues with the user of update_user_meta() in your function.

First, you should never write the value directly from $_POST without sanitizing it first. That's a good way to open yourself up to exploits. (Note: you didn't indicate the field types, so I'm going to assume they are text input - you'll need to adjust accordingly.)

Next, the last argument of update_user_meta() (where you have "true" in the first one, and "false" in the second), is for the previous value to compare. You're registering a new user so these fields would not have an existing value. Leave this argument out.

The last field you're inputting is problematic. As written, you're using the input value as the field meta key. If it's a text field, that means the meta key could be anything the user enters. The meta key should be static and probably match up with the field name in some way (like the previous fields).

Lastly, user_register only passes the $user_id argument. Not sure where you got $password and $meta from, but these are not valid arguments for the hook.

Here's a blind attempt at correcting these issues:

add_action('user_register', 'register_extra_fields');
function register_extra_fields ( $user_id ) {
    update_user_meta( $user_id, 'vardas', sanitize_text_field( $_POST['user_register_vardas'] ) );
    update_user_meta( $user_id, 'pavarde', sanitize_text_field( $_POST['user_register_pavarde'] ) );
    update_user_meta( $user_id, 'telefonas', sanitize_text_field( $_POST['user_register_vardas'] ) );
    update_user_meta( $user_id, 'user_register_vardas', sanitize_text_field( $_POST['user_register_vardas'] ) );
}
  • thanks for spotting sanitization issue, the last field was just to test if it would create a field of a $_POST value and it doesn't but it creates those 3 fields above without values that means that $_POST[] doesn't have any value and I don't understand why – Žilvinas Mar 23 at 3:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.