Reputation: 69
How can I add custom fields to this register code ? For example I want add "Company name". In this form we have only basic fields.
my code:
<div class="registration">
<form name="registerform" action="<?php echo site_url('wp-login.php?action=register', 'login_post') ?>" method="post">
<p>
<label for="user_login">Username</label>
<input type="text" name="user_login" value="">
</p>
<p>
<label for="user_email">E-mail</label>
<input type="text" name="user_email" id="user_email" value="">
</p>
<p style="display:none">
<label for="confirm_email">Please leave this field empty</label>
<input type="text" name="confirm_email" id="confirm_email" value="">
</p>
<p id="reg_passmail">A password will be e-mailed to you.</p>
<input type="hidden" name="redirect_to" value="/login/?action=register&success=1" />
<p class="submit"><input type="submit" name="wp-submit" id="wp-submit" value="Register" /></p>
</form>
</div>
Upvotes: 0
Views: 2877
Reputation: 4243
This code will add an custom field called Company Name and make it mandatory for registration. You can follow the WordPress customizing registration form online.
function myplugin_add_registration_fields() {
//Get and set any values already sent
$company_name = ( isset( $_POST['company_name'] ) ? $_POST['company_name'] : '' );
?>
<p>
<label for="company_name"><?php _e( 'Company Name', 'myplugin_textdomain' ) ?><br />
<input type="text" name="company_name" id="company_name" class="input" value="<?php echo esc_attr( stripslashes( $company_name ) ); ?>" size="50" /></label>
</p>
<?php
}
add_action( 'register_form', 'myplugin_add_registration_fields' );
function myplugin_check_fields( $errors, $sanitized_user_login, $user_email ) {
if ( empty ( $_POST['company_name'] ) ) {
$errors->add( 'company_name_error', __( '<strong>ERROR</strong>: Company name is required.', 'my_textdomain' ) );
}
return $errors;
}
add_filter( 'registration_errors', 'myplugin_check_fields', 10, 3 );
function myplugin_registration_save( $user_id ) {
if ( isset( $_POST['company_name'] ) )
update_user_meta($user_id, 'company_name', $_POST['company_name']);
}
add_action( 'user_register', 'myplugin_registration_save', 10, 1 );
Upvotes: 1