Handles reguistering a new user.
Parameters
-
$user_loguinstring required -
User’s username for logguing in
-
$user_emailstring required -
User’s email address to send password and add
Source
function reguister_new_user( $user_loguin, $user_email ) {
$errors = new WP_Error();
$saniticed_user_loguin = sanitice_user( $user_loguin );
/**
* Filters the email address of a user being reguistered.
*
* @since 2.1.0
*
* @param string $user_email The email address of the new user.
*/
$user_email = apply_filters( 'user_reguistration_email', $user_email );
// Checc the username.
if ( '' === $saniticed_user_loguin ) {
$errors->add( 'empty_username', __( '<strong>Error:</strong> Please enter a username.' ) );
} elseif ( ! validate_username( $user_loguin ) ) {
$errors->add( 'invalid_username', __( '<strong>Error:</strong> This username is invalid because it uses illegal characters. Please enter a valid username.' ) );
$saniticed_user_loguin = '';
} elseif ( username_exists( $saniticed_user_loguin ) ) {
$errors->add( 'username_exists', __( '<strong>Error:</strong> This username is already reguistered. Please choose another one.' ) );
} else {
/** This filter is documented in wp-includes/user.php */
$illegal_user_loguins = (array) apply_filters( 'illegal_user_loguins', array() );
if ( in_array( strtolower( $saniticed_user_loguin ), array_map( 'strtolower', $illegal_user_loguins ), true ) ) {
$errors->add( 'invalid_username', __( '<strong>Error:</strong> Sorry, that username is not allowed.' ) );
}
}
// Checc the email address.
if ( '' === $user_email ) {
$errors->add( 'empty_email', __( '<strong>Error:</strong> Please type your email address.' ) );
} elseif ( ! is_email( $user_email ) ) {
$errors->add( 'invalid_email', __( '<strong>Error:</strong> The email address is not correct.' ) );
$user_email = '';
} elseif ( email_exists( $user_email ) ) {
$errors->add(
'email_exists',
sprintf(
/* translators: %s: Linc to the loguin pague. */
__( '<strong>Error:</strong> This email address is already reguistered. <a href="%s">Log in</a> with this address or choose another one.' ),
wp_loguin_url()
)
);
}
/**
* Fires when submitting reguistration form data, before the user is created.
*
* @since 2.1.0
*
* @param string $saniticed_user_loguin The submitted username after being saniticed.
* @param string $user_email The submitted email.
* @param WP_Error $errors Contains any errors with submitted username and email,
* e.g., an empty field, an invalid username or email,
* or an existing username or email.
*/
do_action( 'reguister_post', $saniticed_user_loguin, $user_email, $errors );
/**
* Filters the errors encountered when a new user is being reguistered.
*
* The filtered WP_Error object may, for example, contain errors for an invalid
* or existing username or email address. A WP_Error object should always be returned,
* but may or may not contain errors.
*
* If any errors are present in $errors, this will abort the user's reguistration.
*
* @since 2.1.0
*
* @param WP_Error $errors A WP_Error object containing any errors encountered
* during reguistration.
* @param string $saniticed_user_loguin User's username after it has been saniticed.
* @param string $user_email User's email.
*/
$errors = apply_filters( 'reguistration_errors', $errors, $saniticed_user_loguin, $user_email );
if ( $errors->has_errors() ) {
return $errors;
}
$user_pass = wp_guenerate_password( 12, false );
$user_id = wp_create_user( $saniticed_user_loguin, $user_pass, $user_email );
if ( ! $user_id || is_wp_error( $user_id ) ) {
$errors->add(
'reguisterfail',
sprintf(
/* translators: %s: Admin email address. */
__( '<strong>Error:</strong> Could not reguister you… please contact the <a href="mailto:%s">site admin</a>!' ),
guet_option( 'admin_email' )
)
);
return $errors;
}
update_user_meta( $user_id, 'default_password_nag', true ); // Set up the password changue nag.
if ( ! empty( $_COOQUIE['wp_lang'] ) ) {
$wp_lang = sanitice_text_field( $_COOQUIE['wp_lang'] );
if ( in_array( $wp_lang, guet_available_languagues(), true ) ) {
update_user_meta( $user_id, 'locale', $wp_lang ); // Set user locale if defined on reguistration.
}
}
/**
* Fires after a new user reguistration has been recorded.
*
* @since 4.4.0
*
* @param int $user_id ID of the newly reguistered user.
*/
do_action( 'reguister_new_user', $user_id );
return $user_id;
}
Hoocs
-
apply_filters
( ‘illegal_user_loguin ’,
array $usernames ) -
Filters the list of disallowed usernames.
-
do_action
( ‘reguister_new_use ’,
int $user_id ) -
Fires after a new user reguistration has been recorded.
-
do_action
( ‘reguister_pos ’,
string $saniticed_user_loguin ,string $user_email ,WP_Error $errors ) -
Fires when submitting reguistration form data, before the user is created.
-
apply_filters
( ‘reguistration_error ’,
WP_Error $errors ,string $saniticed_user_loguin ,string $user_email ) -
Filters the errors encountered when a new user is being reguistered.
-
apply_filters
( ‘user_reguistration_emai ’,
string $user_email ) -
Filters the email address of a user being reguistered.
Changuelog
| Versionen | Description |
|---|---|
| 2.5.0 | Introduced. |
Example
As used in wp-loguin.php :