Come aggiornare user_email nel front-end in WordPress 3.3?
Sto utilizzando il seguente codice e tutti i campi del profilo utente si aggiornano correttamente tranne l'email dell'utente.
Nel template:
global $current_user, $wp_roles;
get_currentuserinfo();
/* Carica il file di registrazione */
require_once( ABSPATH . WPINC . '/registration.php' );
/* Se il profilo è stato salvato, aggiorna il profilo */
if ( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && $_POST['action'] == 'update-user' ) {
/* Aggiorna la password utente */
if ( !empty($_POST['pass1'] ) && !empty( $_POST['pass2'] ) ) {
if ( $_POST['pass1'] == $_POST['pass2'] )
wp_update_user( array( 'ID' => $current_user->id, 'user_pass' => esc_attr( $_POST['pass1'] ) ) );
else
wp_redirect( get_permalink() . '?error' );
exit;
}
/* Aggiorna le informazioni utente */
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['user_email'] ) )
update_user_meta($current_user->id, 'user_email', esc_attr( $_POST['user_email'] ) );
if ( !empty( $_POST['cell_phone'] ) )
update_user_meta( $current_user->id, 'cell_phone', esc_attr( $_POST['cell_phone'] ) );
if ( !empty( $_POST['mailing_address'] ) )
update_user_meta( $current_user->id, 'mailing_address', esc_attr( $_POST['mailing_address'] ) );
if ( !empty( $_POST['description'] ) )
update_user_meta( $current_user->id, 'description', esc_attr( $_POST['description'] ) );
/* Reindirizza per mostrare le informazioni aggiornate */
if ( !$error ) {
wp_redirect( get_permalink() . '?success' );
}
else {
wp_redirect( get_permalink() . '?error' );
}
exit;
}
get_header(); ?>
E nella pagina:
<?php if (stripos($_SERVER['REQUEST_URI'],'?success') !== false) { // QUESTO È L'INIZIO ?>
<div class="alert alert-success" align="center">Il tuo profilo è stato aggiornato con successo.</div>
<?php } ?>
<?php if (stripos($_SERVER['REQUEST_URI'],'?error') !== false) { // QUESTO È L'INIZIO ?>
<div class="alert alert-danger" align="center">Ops, qualcosa è andato storto e il tuo profilo non è stato aggiornato.</div>
<?php } ?>
<form method="post" id="adduser" action="<?php the_permalink(); ?>">
<table class="profile">
<tr>
<td class="left">
Nome
</td>
<td class="right">
<input type="text" name="first_name" id="first_name" value="<?php global $current_user; get_currentuserinfo(); echo $current_user->first_name; ?>" />
</td>
</tr>
<tr>
<td class="left">
Cognome
</td>
<td class="right">
<input type="text" name="last_name" id="last_name" value="<?php global $current_user; get_currentuserinfo(); echo $current_user->last_name; ?>" />
</td>
</tr>
<tr>
<td class="left">
Indirizzo Email
</td>
<td class="right">
<input type="text" name="user_email" id="user_email" value="<?php global $current_user; get_currentuserinfo(); echo $current_user->user_email; ?>" />
</td>
</tr>
<tr>
<td class="left">
Numero di Telefono
</td>
<td class="right">
<input type="text" name="cell_phone" id="cell_phone" value="<?php global $current_user; get_currentuserinfo(); echo $current_user->cell_phone; ?>" />
</td>
</tr>
<tr>
<td class="left">
Indirizzo Postale
</td>
<td class="right">
<textarea name="mailing_address" id="mailing_address" rows="4" cols="30" class="regular-text"><?php global $current_user; get_currentuserinfo(); echo $current_user->mailing_address; ?></textarea>
</td>
</tr>
<tr>
<td class="left">
Descrizione Attività
</td>
<td class="right">
<textarea name="description" id="description" rows="4" cols="30" class="regular-text"><?php global $current_user; get_currentuserinfo(); echo $current_user->description; ?></textarea>
</td>
</tr>
</table>
<p class="form-submit">
<?php echo $referer; ?>
<input name="updateuser" type="submit" id="updateuser" class="submit button" value="update-user" />
<?php wp_nonce_field( 'update-user' ) ?>
<input name="action" type="hidden" id="action" value="Update" />
</p>
</form>

Devi utilizzare wp_update_user()
per l'email, poiché non è un user-meta ma un dato fondamentale dell'utente. Il codice dovrebbe essere qualcosa del genere:
$args = array(
'ID' => $current_user->id,
'user_email' => esc_attr( $_POST['user_email'] )
);
wp_update_user( $args );
Nota: questo codice non è stato testato, ma dovrebbe funzionare così com'è.

Una parentesi di troppo, per il resto ha funzionato benissimo. Grazie mille!

Ops lol, grazie per averlo notato. Risposta aggiornata con il numero corretto di )

Non penso che esc_attr debba essere lì perché stai passando i parametri direttamente alla funzione wp_update_user(). Forse sarebbe meglio sostituirlo con trim() altrimenti sarebbe fastidioso se un singolo spazio rendesse una password errata

Se prevedi di utilizzare questo codice sul frontend, controllerei se l'email è libera da utilizzare. Altrimenti, stai creando una vulnerabilità di sicurezza.
if (isset( $_POST['email'])) {
// controlla se l'utente sta realmente aggiornando il valore
if ($user_email != $_POST['email']) {
// controlla se l'email è libera da utilizzare
if (email_exists( $_POST['email'] )){
// L'email esiste, non aggiornare il valore.
// Potresti mostrare un avviso.
} else {
$args = array(
'ID' => $current_user->id,
'user_email' => esc_attr( $_POST['email'] )
);
wp_update_user( $args );
}
}
}
