Using Giri's function and information from this link http://codex.wordpress.org/Customizing_the_Registration_Form
I managed to end up with the following function to create a Custom Registration for my WordPress site. The user chooses his own password, also specifies Name and Last Names.
Username equals e-mail, since I don't want usernames.
In a separate file (where the register form is) check if the form has been submited and call this function with all the parameters required. Then handle errors and succesful submits.
Inside functions.php file I added the following:
function custom_register_new_user( $user_login, $user_email, $user_apellidos, $user_nombre, $user_password, $user_repassword ) {
$errors = new WP_Error();
$sanitized_user_login = sanitize_user( $user_login );
$user_email = apply_filters( 'user_registration_email', $user_email );
$user_nombre = escape($user_nombre);
$user_password = escape($user_password);
$user_repassword = escape($user_repassword);
// Check the username
if ( $sanitized_user_login == '' ) {
$errors->add( 'empty_username', __( 'Por favor escribí un <strong>E-mail</strong>' ) );
} elseif ( ! validate_username( $user_login ) ) {
$errors->add( 'invalid_username', __( 'El <strong>e-mail</strong> es incorrecto' ) );
$sanitized_user_login = '';
} elseif ( username_exists( $sanitized_user_login ) ) {
$errors->add( 'username_exists', __( 'Este <strong>e-mail</strong> ya está registrado, elegí otro' ) );
}
// Check the e-mail address
if ( $user_email == '' ) {
$errors->add( 'empty_email', 'Por favor escribí un <strong>E-mail</strong>' );
} elseif ( ! is_email( $user_email ) ) {
$errors->add( 'invalid_email', 'El <strong>e-mail</strong> es incorrecto' );
$user_email = '';
} elseif ( email_exists( $user_email ) ) {
$errors->add( 'email_exists', 'Este <strong>e-mail</strong> ya está registrado, elegí otro' );
}
// Check the apellidos
if ( $user_apellidos == '') {
$errors->add( 'apellidos_error', 'Por favor escribí tus <strong>Apellidos</strong>' );
}
// Check the nombre
if ( $user_nombre == '') {
$errors->add( 'nombre_error', 'Por favor escribí tu <strong>Nombre</strong>' );
}
// Check the password
if( $user_password == '' ) {
$errors->add ( 'password_error', 'Por favor escribí una <strong>Contraseña</strong>' );
}
elseif( !(ctype_alnum($user_password)) ) {
$errors->add ( 'password_error', '<strong>Contraseña</strong> no valida. Usá solo letras y números' );
}
elseif( $user_repassword == '' || $user_password != $user_password ) {
$errors->add ( 'password_error', 'Las <strong>Constraseñas</strong> no coinciden</strong>' );
}
do_action( 'register_post', $sanitized_user_login, $user_email, $errors );
$errors = apply_filters( 'registration_errors', $errors, $sanitized_user_login, $user_email, $user_nombre, $user_apellidos, $user_password );
if ( $errors->get_error_code() )
return $errors;
$user_pass = $user_password;
$user_id = wp_create_user( $sanitized_user_login, $user_pass, $user_email );
if ( ! $user_id ) {
$errors->add( 'registerfail', sprintf( __( '<strong>ERROR</strong>: Couldn’t register you... please contact the <a href="mailto:%s">webmaster</a> !' ), get_option( 'admin_email' ) ) );
return $errors;
}
update_user_meta($user_id, 'first_name', $user_nombre );
update_user_meta($user_id, 'last_name', $user_apellidos );
/* wp_new_user_notification( $user_id, $user_pass ); */
return $user_id;
}
To handle errors, check if the function's Return is_wp_error. Else continue with a welcome message or something.
$return = custom_register_new_user($_POST['email'], $_POST['email'], $_POST['apellidos'], $_POST['nombre'], $_POST['password'], $_POST['repassword']);
if( is_wp_error($return) ) {
$errores = $return->errors;
if( count($errores) > 0 ) {
if( $errores['apellidos_error'] )
$error_apellidos = $errores['apellidos_error'][0];
if( $errores['nombre_error'] )
$error_nombre = $errores['nombre_error'][0];
....
etc
....
Also, added "Header" redirect at the deafult registration form, so users will always land on my custom form. Same for Login and Forgotten password