我正在使用Types插件创建用户元数据库,但它不会保存。我的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] );
}
}
}
}
我的类型后端是:
SO网友:inarilo
可以使用为同一个键添加多个值add_user_meta
设置最后一个参数$unique
到false
:
for($i=0; $i < $count; $i++){
add_user_meta($user_id, \'wpcf-sectores-de-experiencia-u\', $sectores[$i], false);
}
如果您希望使用
update_user_meta
您可以为每个复选框使用单独的键:
for($i=0; $i < $count; $i++){
update_user_meta($user_id, $sectores[$i], \'1\');
}
但是,由于未选中的复选框不会提交,更新将必须检查丢失的密钥并将其删除。因此,更简单的方法是直接保存阵列:
update_user_meta($user_id, \'wpcf-sectores-de-experiencia-u\', $sectores);
您可以通过使用
in_array
:
$sectores = get_user_meta($user_id, \'wpcf-sectores-de-experiencia-u\', true);
if(in_array(\'clases-dirigidas\', $sectores)) {
//do something
}