Created
June 8, 2012 10:56
-
-
Save alessandro-fazzi/2894983 to your computer and use it in GitHub Desktop.
Wordpress Custom registration form width connected custom user profile's meta
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Queste action aggiungono nel profilo utente i campi personalizzati. | |
* L'HTML è ovviamente da personalizzare a piacere. | |
* $user è una variabile passata in automatico dalla action di WP. | |
* 'custom_info' è la parte personalizzabile...si dovrebbe capire come e dove ;) | |
***/ | |
add_action( 'show_user_profile', 'my_show_custom_user_info' ); | |
add_action( 'edit_user_profile', 'my_show_custom_user_info' ); | |
function my_show_custom_user_info($user) { | |
$html = ' | |
<h3>Extra profile information</h3> | |
<table class="form-table"> | |
<tr> | |
<th><label for="custom_info">Custom Info</label></th> | |
<td> | |
<input type="text" name="custom_info" id="custom_info" value="'.esc_attr( get_the_author_meta( 'custom_info', $user->ID ) ).'" class="regular-text" /><br /> | |
<span class="description">Please enter your custom info.</span> | |
</td> | |
</tr> | |
</table>'; | |
echo $html; | |
} | |
/* | |
* Queste gestiscono il salvataggio delle informazioni aggiuntive gestite | |
* nei form di cui sopra | |
***/ | |
add_action( 'personal_options_update', 'my_save_custom_user_info' ); | |
add_action( 'edit_user_profile_update', 'my_save_custom_user_info' ); | |
function my_save_custom_user_info( $user_id ) { | |
//Questo controllo che WP consiglia a noi è dannoso, perché | |
//al momento della registrazione l'utente non può ancora modificarsi | |
//i dati...almeno mi pare. Di contro questi campi saranno poi SEMPRE | |
//modificabili dagli utenti nel loro profilo. | |
// if ( !current_user_can( 'edit_user', $user_id ) ) | |
// return false; | |
update_usermeta( $user_id, 'custom_info', $_POST['custom_info'] ); | |
} | |
/* | |
* Questa action aggiunge campi al form di registrazione. HTML personalizzabile | |
***/ | |
add_action('register_form','registration_form_custom_info'); | |
function registration_form_custom_info(){ | |
$html = ' | |
<p> | |
<label for="custom_info">'._e("Custom Info").'<br /> | |
<input type="custom" name="custom_info" id="custom_info" class="input" value="'.$_POST['custom_info'].'" size="25" tabindex="20" /></label> | |
</p> | |
'; | |
echo $html; | |
} | |
/* | |
* Questa action salva i campi personalizzati del form di registrazione la prima volta che | |
* ci si registra. ATTENZIONE: nelle funzioni my_save_custom_user_info e registration_form_custom_info | |
* ci deve essere una corrispondenza delle variabili passate in $_POST. Negli esempi ho usato sempre 'custom_info'. | |
***/ | |
add_action('user_register','my_save_custom_user_info'); | |
//Tutte le informazioni salvate in questa maniera sono poi modificabili dall'utente | |
//all'indirizzo http://greenline-int.com/wp-admin/profile.php e utilizzabili nel codice: | |
// retrive: get the author meta (http://codex.wordpress.org/Function_Reference/get_the_author_meta) | |
// update: update_usermeta (http://codex.wordpress.org/Function_Reference/update_user_meta) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment