Come aggiungere nome e cognome al modulo di registrazione predefinito?
26 mar 2017, 14:35:42
Visualizzazioni: 23.3K
Voti: 5
È possibile aggiungere nome e cognome al modulo di registrazione predefinito di WordPress?

Archangel17
489
Tutte le risposte alla domanda
3
0
Aggiungi questo codice in functions.php
add_action( 'register_form', 'myplugin_register_form' );
function myplugin_register_form() {
$first_name = ( ! empty( $_POST['first_name'] ) ) ? trim( $_POST['first_name'] ) : '';
$last_name = ( ! empty( $_POST['last_name'] ) ) ? trim( $_POST['last_name'] ) : '';
?>
<p>
<label for="first_name"><?php _e( 'Nome', 'mydomain' ) ?><br />
<input type="text" name="first_name" id="first_name" class="input" value="<?php echo esc_attr( wp_unslash( $first_name ) ); ?>" size="25" /></label>
</p>
<p>
<label for="last_name"><?php _e( 'Cognome', 'mydomain' ) ?><br />
<input type="text" name="last_name" id="last_name" class="input" value="<?php echo esc_attr( wp_unslash( $last_name ) ); ?>" size="25" /></label>
</p>
<?php
}
//2. Aggiungi validazione. In questo caso, rendiamo obbligatori nome e cognome.
add_filter( 'registration_errors', 'myplugin_registration_errors', 10, 3 );
function myplugin_registration_errors( $errors, $sanitized_user_login, $user_email ) {
if ( empty( $_POST['first_name'] ) || ! empty( $_POST['first_name'] ) && trim( $_POST['first_name'] ) == '' ) {
$errors->add( 'first_name_error', __( '<strong>ERRORE</strong>: Devi inserire un nome.', 'mydomain' ) );
}
if ( empty( $_POST['last_name'] ) || ! empty( $_POST['last_name'] ) && trim( $_POST['last_name'] ) == '' ) {
$errors->add( 'last_name_error', __( '<strong>ERRORE</strong>: Devi inserire un cognome.', 'mydomain' ) );
}
return $errors;
}
//3. Infine, salviamo i metadati aggiuntivi dell'utente durante la registrazione.
add_action( 'user_register', 'myplugin_user_register' );
function myplugin_user_register( $user_id ) {
if ( ! empty( $_POST['first_name'] ) ) {
update_user_meta( $user_id, 'first_name', trim( $_POST['first_name'] ) );
update_user_meta( $user_id, 'last_name', trim( $_POST['last_name'] ) );
}
}
Per maggiori informazioni vedi codex
Puoi anche utilizzare alcuni plugin

Faysal Mahamud
1.04K
26 mar 2017 15:14:05
0
Il codice funziona, ma ho individuato un piccolo bug. Nella validazione del last_name, l'ultima variabile nella condizione if ha "first_name", e dovrebbe avere "last_name". Quindi quel pezzo di codice, corretto, sarebbe:
if ( empty( $_POST['last_name'] ) || ! empty( $_POST['last_name'] ) && trim( $_POST['last_name'] ) == '' ) {
$errors->add( 'last_name_error', __( '<strong>ERRORE</strong>: Devi inserire un cognome.', 'mydomain' ) );
}

Ignasi Calvo
1
25 gen 2018 19:55:45
0
$form['account']['lname'] = array('#type' => 'textfield',
'#title' => t('Cognome'),
'#default_value' => $edit['LastName'],
'#maxlength' => LASTNAME_MAX_LENGTH,
'#description' => t('Gli spazi sono consentiti; la punteggiatura non è ammessa tranne punti, trattini e underscore.'),
'#required' => TRUE,
);
Puoi farlo solo tramite l'interfaccia utente
Naviga su sitedomain/admin/config/people/accounts/fields
e aggiungi il nome e il cognome come campi, verranno visualizzati nel modulo di registrazione predefinito

Ethan Root
1
15 giu 2018 16:25:49
Domande correlate
2
risposte