-1

I'm using Types plugin to create user metaboxes but it doesn't save. My HTML:

<form id="new-user" action="" method="POST" name="new-user" novalidate="" enctype="multipart/form-data">
    <div class="form-group">
      <input type="text" name="wpcf-nombre-u" id="nombre" placeholder="<?php _e('Nombre', 'wm'); ?>" >
    </div>
    <div class="form-group">
      <input type="text" name="wpcf-apellidos-u" id="apellidos" placeholder="<?php _e('Apellidos', 'wm'); ?>" >
    </div>
    <div class="form-group">
      <input type="password" name="wpcf-contrasena-u" id="contrasena" placeholder="<?php _e('Contraseña', 'wm'); ?>" >
    </div>
    <div class="form-group">
      <input type="password" name="wpcf-rep-contrasena-u" id="rep-contrasena" placeholder="<?php _e('Repita contraseña', 'wm'); ?>" >
    </div>
    <div class="form-group">
      <input type="email" name="wpcf-email-u" id="email" placeholder="Email" >
    </div>  
    <div class="form-group">
        <input type="checkbox" name="sectores[]" id="sector-nutricion-deportiva" value="nutricion-deportiva">
        <label for="sector-nutricion-deportiva">Nutrición deportiva</label>

        <input type="checkbox" name="sectores[]" id="sector-rutinas-de-gimnasio" value="rutinas-de-gimnasio">
        <label for="sector-rutinas-de-gimnasio">Rutinas de gimnasio</label>

        <input type="checkbox" name="sectores[]" id="sector-clases-dirigidas" value="clases-dirigidas">
        <label for="sector-clases-dirigidas">Clases dirigidas</label>

        <input type="checkbox" name="sectores[]" id="sector-entrenamiento-personal" value="entrenamiento-personal">
        <label for="sector-entrenamiento-personal">Entrenamiento personal</label>

        <input type="checkbox" name="sectores[]" id="sector-deportes-de-equipo" value="deportes-de-equipo">
        <label for="sector-deportes-de-equipo">Deportes de equipo</label>

        <input type="checkbox" name="sectores[]" id="sector-deportes-individuales" value="deportes-individuales">
        <label for="sector-deportes-individuales">Deportes individuales</label>

        <input type="submit" value="¡Registrate ya!" />
        <input type="hidden" name="task" value="register" />

    </div>
</form>

PHP:

$err = '';
$success = '';

global $wpdb, $PasswordHash, $current_user, $user_ID;

if( isset($_POST['task']) && $_POST['task'] == 'register' ) {
  $nombre = $wpdb->escape(trim($_POST['wpcf-nombre-u']));
  $apellidos = $wpdb->escape(trim($_POST['wpcf-apellidos-u']));
  $contrasena = $wpdb->escape(trim($_POST['wpcf-contrasena-u']));
  $rep_contrasena = $wpdb->escape(trim($_POST['wpcf-rep-contrasena-u']));
  $email = $wpdb->escape(trim($_POST['wpcf-email-u']));

  if( $email == "" || $contrasena == "" || $rep_contrasena == "" || $username == "" || $nombre == "" || $apellidos == "") {
    $err = 'Please don\'t leave the required fields.';
  } else if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    $err = 'Invalid email address.';
  } else if(email_exists($email) ) {
    $err = 'Email already exist.';
  } else if($contrasena <> $rep_contrasena ){
    $err = 'Password do not match.';
  } else {
    $user_id = wp_insert_user( 
        array (
            'first_name' => apply_filters('pre_user_first_name', $nombre), 
            'last_name' => apply_filters('pre_user_last_name', $apellidos), 
            'user_pass' => apply_filters('pre_user_user_pass', $contrasena), 
            'user_login' => apply_filters('pre_user_user_login', $username),
            'user_email' => apply_filters('pre_user_user_email', $email), 'role' => 'subscriber' 
        ) 
    );

    if( is_wp_error($user_id) ) {
        $err = 'Error on user creation.';
    } else {
        $success = 'You\'re successfully register';
        $sectores = $_POST['sectores'];
        $count = count($sectores);
        for($i=0; $i < $count; $i++){
          update_user_meta( $user_id, 'wpcf-sectores-de-experiencia-u', $sectores[$i] );
        }
    }
  }
}

My types backend is: enter image description here

  • please provide more complete code, where is this code called, which function / hook are you using? – inarilo Aug 22 '17 at 22:35
  • Sorry, I edit my code – Carlos GR Aug 23 '17 at 5:34
  • is the user registered successfully? are the other fields used in the code part of the same form? – inarilo Aug 23 '17 at 7:44
  • The user is registered and the others fields are saving successfully. The other fields are in the same form. I change the HTML to show them. – Carlos GR Aug 23 '17 at 9:10
  • don;t call do_action('user_register', $user_id);, that is handled by developer.wordpress.org/reference/functions/wp_insert_user. the userdata may be saving just the last value because you are using the same key. – inarilo Aug 23 '17 at 19:21
0

Remove apply_filters() and just pass the array values into your wp_insert_user:

array(
   'first_name' => $nombre, 
   'last_name'  => $apellidos, 
   'user_pass'  => $contrasena, 
   'user_login' => $username,
   'user_email' => $email,
   'role'       => 'subscriber',
) 

WordPress already managed those apply_filters, so there's no reason to use them in your code.

Hope it helps! Me dices si te sirvió, también hablo español. :)

0

You can add multiple values for the same key by using add_user_meta and setting the last parameter $unique to false:

for($i=0; $i < $count; $i++){
  add_user_meta($user_id, 'wpcf-sectores-de-experiencia-u', $sectores[$i], false);
}

If you wish to use update_user_meta you can use separate keys for each checkbox:

for($i=0; $i < $count; $i++){
  update_user_meta($user_id, $sectores[$i], '1');
}

However, since unchecked checkboxes are not submitted, updates will have to check for missing keys and remove them. A simpler way therefore would be to just save the array directly:

update_user_meta($user_id, 'wpcf-sectores-de-experiencia-u', $sectores);

And you can check if a value has been checked by using in_array:

$sectores = get_user_meta($user_id, 'wpcf-sectores-de-experiencia-u', true);
if(in_array('clases-dirigidas', $sectores)) {
  //do something
}

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.