我已经创建了一个表单来更新前端的几个字段,但是我收到了电子邮件和个人资料图片字段的问题。当我更新这些字段时,一切正常,但是当我再次更新表单时,值只会在表单和数据库中消失。
这是只有两个字段和PHP使用的代码(下面是完整代码):
<?php
/* Get user info. */
global $current_user, $wp_roles;
get_currentuserinfo();
/* Load the registration file. */
require_once( ABSPATH . WPINC . '/registration.php' );
$error = array();
/* If profile was saved, update profile. */
if ( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && $_POST['action'] == 'update-user' ) {
if ( !empty( $_POST['user-email'] ) ){
if (!is_email(esc_attr( $_POST['user-email'] )))
$error[] = __('El email que has introducido no es valido, por favor introduce un correo valido.', 'buscopreparador');
elseif(email_exists(esc_attr( $_POST['user-email'] )) != $current_user->id )
$error[] = __('Este email ya esta en uso por otro preparador. intenta utilizando otro.', 'buscopreparador');
else{
wp_update_user( array ('ID' => $current_user->ID, 'user_email' => esc_attr( $_POST['email'] )));
}
}
/* Update ACF Fields */
$user_id_acf = 'user_'.$current_user->ID;
if( !empty( $_FILES['imagen-perfil'] ) ) {
/* Function to pass the image to the media and database */
$att = my_update_attachment('imagen-perfil', $user_id_acf);
update_field('field_56ea616d98e27', $att['attach_id'], $user_id_acf);
}
/* Redirect so the page will show updated info.*/
/*I am not Author of this Code- i dont know why but it worked for me after changing below line to if ( count($error) == 0 ){ */
if ( count($error) == 0 ) {
do_action('personal_options_update', $current_user->ID);
wp_redirect( get_permalink().'?updated=true' ); exit;
}
}
get_header(); ?>
<form method="post" id="adduser" action="<?php the_permalink(); ?>" enctype="multipart/form-data">
<div class="row">
<!-- Email -->
<div class="form-group col-md-6">
<label for="user-email"><?php _e('Email', 'buscopreparador'); ?></label>
<input type="email" class="form-control" name="user-email" id="user-email" placeholder="danielmartinez@gmail.com" value="<?php the_author_meta( 'user_email', $current_user->ID ); ?>">
</div>
<!-- Imagen de Perfil -->
<div class="form-group col-md-6">
<label for="imagen-perfil">Imagen de Perfil</label>
<input type="file" name="imagen-perfil" id="imagen-perfil">
<p class="help-block">Selecciona tu imagen de perfil, esta debe tener entre 200x200 y 400x400 pixeles.</p>
</div>
</div>
<p class="form-submit">
<?php echo $referer; ?>
<input name="updateuser" type="submit" id="updateuser" class="btn btn-primary btn-large" value="<?php _e('Actualizar', 'buscopreparador'); ?>">
<?php wp_nonce_field( 'update-user_'. $current_user->ID ) ?>
<input name="action" type="hidden" id="action" value="update-user" />
</p><!-- .form-submit -->
</form><!-- #adduser -->
这是我更新数据库字段的代码:
<?php
/**
* Template Name: User Profile
*
* Allow users to update their profiles from Frontend.
*/
/* Get user info. */
global $current_user, $wp_roles;
get_currentuserinfo();
/* Load the registration file. */
require_once( ABSPATH . WPINC . '/registration.php' );
$error = array();
/* If profile was saved, update profile. */
if ( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && $_POST['action'] == 'update-user' ) {
/* Update user password. */
if ( !empty($_POST['user-password1'] ) && !empty( $_POST['user-password2'] ) ) {
if ( $_POST['user-password1'] == $_POST['user-password2'] )
wp_update_user( array( 'ID' => $current_user->ID, 'user_pass' => esc_attr( $_POST['user-password1'] ) ) );
else
$error[] = __('Las contraseñas que has introducido no concuerdan. No se ha actualizado su contraseña.', 'buscopreparador');
}
/* Update user website. */
if ( !empty( $_POST['user-web'] ) )
wp_update_user( array ('ID' => $current_user->ID, 'user_url' => esc_attr( $_POST['user-web'] )));
if ( !empty( $_POST['user-email'] ) ){
if (!is_email(esc_attr( $_POST['user-email'] )))
$error[] = __('The Email you entered is not valid. please try again.', 'buscopreparador');
elseif(email_exists(esc_attr( $_POST['user-email'] )) != $current_user->id )
$error[] = __('Este email ya esta en uso por otro preparador. intenta utilizando otro.', 'buscopreparador');
else{
wp_update_user( array ('ID' => $current_user->ID, 'user_email' => esc_attr( $_POST['email'] )));
}
}
if ( !empty( $_POST['first-name'] ) )
update_user_meta( $current_user->ID, 'first_name', esc_attr( $_POST['first-name'] ) );
if ( !empty( $_POST['last-name'] ) )
update_user_meta($current_user->ID, 'last_name', esc_attr( $_POST['last-name'] ) );
if ( !empty( $_POST['nickname'] ) )
update_user_meta($current_user->ID, 'nickname' , esc_attr( $_POST['nickname'] ));
/* Update ACF Fields */
$user_id_acf = 'user_'.$current_user->ID;
if( !empty( $_POST['titulacion-preparador'] ) )
update_field( 'field_56e6c502df6af', esc_attr( $_POST['titulacion-preparador'] ), $user_id_acf );
if( !empty( $_POST['descripcion-breve'] ) )
update_field( 'field_56e6c622df6b2', esc_textarea( $_POST['descripcion-breve'] ), $user_id_acf );
if( !empty( $_POST['materiales-disponibles'] ) )
update_field( 'field_56e939f3d2671', esc_textarea( $_POST['materiales-disponibles'] ), $user_id_acf );
if( !empty( $_FILES['imagen-perfil'] ) ) {
$att = my_update_attachment('imagen-perfil', $user_id_acf);
update_field('field_56ea616d98e27', $att['attach_id'], $user_id_acf);
}
/* Redirect so the page will show updated info.*/
/*I am not Author of this Code- i dont know why but it worked for me after changing below line to if ( count($error) == 0 ){ */
if ( count($error) == 0 ) {
do_action('personal_options_update', $current_user->ID);
wp_redirect( get_permalink().'?updated=true' ); exit;
}
}
这是表格代码:
<form method="post" id="adduser" action="<?php the_permalink(); ?>" enctype="multipart/form-data">
<div class="row">
<!-- Nombre -->
<div class="form-group col-md-6">
<label for="first-name"><?php _e('Nombre', 'buscopreparador'); ?></label>
<input type="text" class="form-control" name="first-name" id="first-name" placeholder="Daniel" value="<?php the_author_meta( 'first_name', $current_user->ID ); ?>">
</div>
<!-- Apellido -->
<div class="form-group col-md-6">
<label for="last-name"><?php _e('Apellidos', 'buscopreparador'); ?></label>
<input type="text" class="form-control" name="last-name" id="last-name" placeholder="Martinez Martinez" value="<?php the_author_meta( 'last_name', $current_user->ID ); ?>">
</div>
<!-- Alias Público -->
<div class="form-group col-md-6">
<label for="alias-publico"><?php _e('Alias Público', 'buscopreparador') ?></label>
<input type="text" class="form-control" name="nickname" id="nickname" placeholder="Daniel Martinez" value="<?php the_author_meta( 'nickname', $current_user->ID ); ?>">
</div>
<!-- Email -->
<div class="form-group col-md-6">
<label for="user-email"><?php _e('Email', 'buscopreparador'); ?></label>
<input type="email" class="form-control" name="user-email" id="user-email" placeholder="danielmartinez@gmail.com" value="<?php the_author_meta( 'user_email', $current_user->ID ); ?>">
</div>
<!-- Web -->
<div class="form-group col-md-6">
<label for="user-web"><?php _e('Web', 'buscopreparador'); ?></label>
<input type="url" class="form-control" name="user-web" id="user-web" value="<?php the_author_meta( 'user_url', $current_user->ID ); ?>" placeholder="http://www.miweb.com">
</div>
<!-- Imagen de Perfil -->
<div class="form-group col-md-6">
<label for="imagen-perfil">Imagen de Perfil</label>
<input type="file" name="imagen-perfil" id="imagen-perfil">
<p class="help-block">Selecciona tu imagen de perfil, esta debe tener entre 200x200 y 400x400 pixeles.</p>
</div>
<!-- Password-1 -->
<div class="form-group col-md-6">
<label for="user-password1"><?php _e('Contraseña', 'buscopreparador'); ?></label>
<input type="password" class="form-control" name="user-password1" id="user-password1">
</div>
<!-- Password-2 -->
<div class="form-group col-md-6">
<label for="user-password2"><?php _e('Repite la Contraseña', 'buscopreparador'); ?></label>
<input type="password" class="form-control" name="user-password2" id="user-password2">
</div>
</div>
<p class="form-submit">
<?php echo $referer; ?>
<input name="updateuser" type="submit" id="updateuser" class="btn btn-primary btn-large" value="<?php _e('Actualizar', 'profile'); ?>">
<?php wp_nonce_field( 'update-user_'. $current_user->ID ) ?>
<input name="action" type="hidden" id="action" value="update-user" />
</p><!-- .form-submit -->
</form><!-- #adduser -->
答案 0 :(得分:0)
为避免发送空图像我使用:
if( !empty( $_FILES['imagen-perfil']['size'] > 0 ) ) {
[&#39;尺寸&#39;]&gt; 0 似乎有用,但是,我不知道为什么。无法解决电子邮件问题。