-1
Ön uçtan birkaç alanı güncellemek için bir form oluşturdum, ancak e-posta ve profil resimleri alanları ile sorunlarım var. Bu alanları güncellediğimde her şey tamam, ama sonra formu tekrar güncellediğimde, değer sadece formda ve veritabanında kaybolur.
<?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;
}
}
Bu: Bu benim kod veritabanı alanlarını güncelleştirmek için
<?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="[email protected]" 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 -->
:
Bu
sadece iki alanlarla kod ve kullanılan PHP (aşağıda tam kodudur) 'dir<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="[email protected]" 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 -->