Custom success message after user is registered

I am using this code as a custom signup form. Is there any chance to create a success message for it?

?php
/*
** Template Name: Custom Register Page
*/
get_header();

global $wpdb, $user_ID;  

    if (isset($_POST['user_registeration']))
    {
        //registration_validation($_POST['username'], $_POST['useremail']);
        global $reg_errors;
        $reg_errors = new WP_Error;
        $username=$_POST['username'];
        $useremail=$_POST['useremail'];
        $password=$_POST['password'];
        
        
        if(empty( $username ) || empty( $useremail ) || empty($password))
        {
            $reg_errors-add('field', 'Required form field is missing');
        }    
        if ( 6  strlen( $username ) )
        {
            $reg_errors-add('username_length', 'Username too short. At least 6 characters is required' );
        }
        if ( username_exists( $username ) )
        {
            $reg_errors-add('user_name', 'The username you entered already exists!');
        }
        if ( ! validate_username( $username ) )
        {
            $reg_errors-add( 'username_invalid', 'The username you entered is not valid!' );
        }
        if ( !is_email( $useremail ) )
        {
            $reg_errors-add( 'email_invalid', 'Email id is not valid!' );
        }
        
        if ( email_exists( $useremail ) )
        {
            $reg_errors-add( 'email', 'Email Already exist!' );
        }
        if ( 5  strlen( $password ) ) {
            $reg_errors-add( 'password', 'Password length must be greater than 5!' );
        }
        
        if (is_wp_error( $reg_errors ))
        { 
            foreach ( $reg_errors-get_error_messages() as $error )
            {
                 $signUpError='p style=color:#FF0000; text-aling:left;strongERROR/strong: '.$error . 'br //p';
            } 
        }
        
        
        if ( 1  count( $reg_errors-get_error_messages() ) )
        {
            // sanitize user form input
            global $username, $useremail;
            $username   =   sanitize_user( $_POST['username'] );
            $useremail  =   sanitize_email( $_POST['useremail'] );
            $password   =   esc_attr( $_POST['password'] );
            
            $userdata = array(
                'user_login'    =   $username,
                'user_email'    =   $useremail,
                'user_pass'     =   $password,
                );
            $user = wp_insert_user( $userdata );
        }
        
    
    } 
    

?


h3Create your account/h3
form action= method=post name=user_registeration
    labelUsername span class=error*/span/label
    input type=text name=username placeholder=Enter Your Username class=text required /br /
    labelEmail address span class=error*/span/label
    input type=text name=useremail class=text placeholder=Enter Your Email required / br /
    labelPassword span class=error*/span/label
    input type=password name=password class=text placeholder=Enter Your password required / br /
    input type=submit name=user_registeration value=SignUp /
/form
?php if(isset($signUpError)){echo 'div'.$signUpError.'/div';}?

Topic wp-signup Wordpress

Category Web


If you look at the function wp_insert_user() in the codex you will see it returns the new users ID if all goes well - or a WP Error Object if it failed.

So you can wrap your form in conditionals to handle the different scenarios.

(Shorthand syntax)
if( is_numeric( $user ) ) {
    <p>Congratualtions on being a new user</p>
} else if( is_wp_error( $user ) ) {
    <p>There's been a problem adding the user</p>
} else {
    <form.........
    // Don't forget the sign up error handling too
}

I will also add that in your original supplied code I can see that you are checking against values that may not be the final values you insert into your DB via wp_insert_user - if you're inserting a sanitized user string then check a sanitized user string. Otherwise this<tag>user is added as thisuser - but you didn't check for that....


an easy way is to redirect form submitter

// when form is submitted :
wp_redirect($your_post_url . '&submitform=1');
exit;

Then you can just test if "submitform" is defined to display what you want :

// test submitform url setting :
if (isset($_GET['submitform'])) 
    echo '<div class="successmessage"><p>' . __('Your Success message.') . '</p></div>';
endif;

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.