Created
June 24, 2013 20:19
-
-
Save makbeta/5853259 to your computer and use it in GitHub Desktop.
WordPress: Add custom profile fields to a Theme My Login registration form
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
/* | |
1. create custom user fields with wordpress (see https://gist.github.com/makbeta/5851535 ) | |
2. clone `register-form.php` from /wp-content/plugins/theme-my-login/templates to your theme | |
3. Add your new field to the registration template | |
*/ | |
<p> | |
<label for="subscription">Customer ID</label> | |
<input type="text" name="subscription" id="subscription<?php $template->the_instance(); ?>" class="input" value="<?php $template->the_posted_value( 'subscription' ); ?>" size="20" /><br /> | |
<span class="description">Please enter your customer ID.</span> | |
</p> | |
/* Add handling of the field during registration by adding the following to your functions.php */ | |
<?php | |
// This function can be used for validation such as required fields | |
function my_registration_errors( $errors, $user_login ) { | |
// Require "my-field" | |
if ( empty( $_POST['subscription'] ) ) | |
$errors->add( 'empty_subscription', __( '"Customer ID" is a required field!' ) ); | |
} | |
// This function will save the custom field to the user meta table | |
function my_new_user_registered( $user_id ) { | |
if ( isset( $_POST['subscription'] ) ) | |
update_user_meta( $user_id, 'subscription', $_POST['subscription'] ); | |
} | |
add_action( 'registration_errors', 'my_registration_errors', 10, 2 ); | |
add_action( 'tml_new_user_registered', 'my_new_user_registered' ); | |
?> |
I just tried this and it broke my site. Ugh. No, worries, it's fixed, but I still need to add a custom field to my Theme-My-Login.
Here is my solution to the problem of adding fields to Theme-My-Login registration form
- add the code to the file functions.php rename the existing WordPress fields to the ones we need
add_filter('user_contactmethods', '_my_get_user_contactmethods');
function _my_get_user_contactmethods() {
$user_contactmethods = array(
'appartment' => __('Numéro de votre appartement'),
'basement_number' => __('Numéro de cave, le cas échéant')
);
return $user_contactmethods;
}
- in the file register-form.php add the output of fields
<label for="appartment<?php $template->the_instance(); ?>"><?php _e( 'Numéro de appartemen', 'theme-my-login' ) ?></label>
<input type="text" name="appartment" id="appartment" class="input" value="<?php echo $_POST['appartment']; ?>" size="15" tabindex="20" />
<label for="basement_number<?php $template->the_instance(); ?>"><?php _e( 'Numéro de chambre au sous-sol', 'theme-my-login' ) ?></label>
<input type="text" name="basement_number" id="basement_number" class="input" value="<?php echo $_POST['basement_number']; ?>" size="15" tabindex="20" />
- in the file functions.php will check for errors the entered data by the user and update the user data :
add_action('register_post','check_fields',10,3);
add_action('user_register', 'register_fields');
function check_fields ( $login, $email, $errors ) {
global $appartment, $basement_number;
if ($_POST['appartment'] == ''){
$errors->add( 'empty_realname', "<strong>ERREUR</strong>: Indiquez le numéro de votre appartment!" );
} else {
$appartment = $_POST['appartment'];
}
if ($_POST['basement_number'] == ''){
$errors->add( 'empty_realname', "<strong>ERREUR</strong>: Indiquez votre numéro de cave, le cas échéant" );
}
else {
$basement_number = $_POST['basement_number'];}
}
function register_fields($user_id,$password= "",$meta=array()){
update_user_meta( $user_id, 'appartment', $_POST['appartment'] );
update_user_meta( $user_id, 'basement_number', $_POST['basement_number'] );
}
this dont work for me...
<label for="basement_number<?php $template->the_instance(); ?>"><?php _e( 'Numéro de chambre au sous-sol', 'theme-my-login' ) ?></label>
<input type="text" name="basement_number" id="basement_number" class="input" value="<?php echo $_POST['basement_number']; ?>" size="15" tabindex="20" />
this work for me...
<?php
foreach ( wp_get_user_contact_methods() as $name => $desc ) {
?>
<tr class="tml-user-contact-method-<?php echo $name; ?>-wrap">
<th><label for="<?php echo $name; ?>"><?php echo apply_filters( 'user_'.$name.'_label', $desc ); ?></label></th>
<td><input type="text" name="<?php echo $name; ?>" id="<?php echo $name; ?>" value="<?php echo esc_attr( $profileuser->$name ); ?>" class="shadow regular-text" /></td>
</tr>
<?php
}
?>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
add line 19 / 20 "return $errors;"