0

How we can get the success messages like we can get the error messages by simple using the variable $error . So there is any other variable like $success. Currently i am using this code to register a new user. so after registeration i want to display success message.

if ( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && $_POST['action'] == 'adduser' ) {
    $user_pass = wp_generate_password();
    $userdata = array(
        'user_login' => esc_attr( $_POST['user_name'] ),
        'user_pass' => esc_attr( $_POST['pass1'] ),
        'user_email' => esc_attr( $_POST['email'] ),
        'user_url' => esc_attr( $_POST['website'] ),
        'description' => esc_attr( $_POST['description'] ),
        'role' => get_option( 'default_role' ),
    );

    if ( !$userdata['user_login'] )
        $error = __('A username is required for registration.', 'frontendprofile');
    elseif ( username_exists($userdata['user_login']) )
        $error = __('Sorry, that username already exists!', 'frontendprofile');

    elseif ( !is_email($userdata['user_email'], true) )
        $error = __('You must enter a valid email address.', 'frontendprofile');
    elseif ( email_exists($userdata['user_email']) )
        $error = __('Sorry, that email address is already used!', 'frontendprofile');
    else if ( $_POST['pass1'] !== $_POST['pass2'] ) {
        $error = __('Sorry, Passwords must match', 'frontendprofile');
    }
    else if ( strlen( $_POST['pass1'] ) < 6 ) {
        $error = __('Sorry, Passwords must be at least six characters long', 'frontendprofile');
    }
    else{
        $new_user = wp_insert_user( $userdata );
        wp_new_user_notification($new_user, $user_pass);
    }
}

1 Answer 1

0

You can declare $success variable and use it to display success message as following.

Declare $success variable and assign success message to it if user is registered successfully as following :

$success = '';
if ( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && $_POST['action'] == 'adduser' ) {
$user_pass = wp_generate_password();
$userdata = array(
    'user_login' => esc_attr( $_POST['user_name'] ),
    'user_pass' => esc_attr( $_POST['pass1'] ),
    'user_email' => esc_attr( $_POST['email'] ),
    'user_url' => esc_attr( $_POST['website'] ),
    'description' => esc_attr( $_POST['description'] ),
    'role' => get_option( 'default_role' ),
);

if ( !$userdata['user_login'] )
    $error = __('A username is required for registration.', 'frontendprofile');
elseif ( username_exists($userdata['user_login']) )
    $error = __('Sorry, that username already exists!', 'frontendprofile');

elseif ( !is_email($userdata['user_email'], true) )
    $error = __('You must enter a valid email address.', 'frontendprofile');
elseif ( email_exists($userdata['user_email']) )
    $error = __('Sorry, that email address is already used!', 'frontendprofile');
else if ( $_POST['pass1'] !== $_POST['pass2'] ) {
    $error = __('Sorry, Passwords must match', 'frontendprofile');
}
else if ( strlen( $_POST['pass1'] ) < 6 ) {
    $error = __('Sorry, Passwords must be at least six characters long', 'frontendprofile');
}
else{
    $new_user = wp_insert_user( $userdata );
 if (!is_wp_error($return) ){
    $success = __($userdata['user_login'].' User registered $userdata['user_login'].', 'frontendprofile');
    wp_new_user_notification($new_user, $user_pass);
}
}
}

And you can use that $success variable to display message as following:

if($success)
     echo $success;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.