create registration form ajax with verifiercation code

Home » Snippets » create registration form ajax with verifiercation code
0

Created with:

Visibility: 

public

Creator: majed

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project
					<?php
/**
 * Plugin Name: WP Plugin Example
 * Description: Example plugin for creating a registration form with Ajax and verification code.
 * Version: 1.0
 * Author: Your Name
 * Author URI: https://your-website.com
 * Text Domain: wpturbo
 */

// Enqueue scripts and styles
add_action( 'wp_enqueue_scripts', 'wpturbo_enqueue_scripts' );
function wpturbo_enqueue_scripts() {
    wp_enqueue_script( 'wpturbo-registration', plugin_dir_url( __FILE__ ) . 'js/registration.js', array( 'jquery' ), '1.0', true );
    wp_localize_script( 'wpturbo-registration', 'wpturbo_ajax_object', array(
        'ajax_url' => admin_url( 'admin-ajax.php' ),
        'nonce'    => wp_create_nonce( 'wpturbo_registration_nonce' ),
    ) );
}

// Register the shortcode for the registration form
add_shortcode( 'registration_form', 'wpturbo_registration_form_shortcode' );

/**
 * Shortcode callback function for the registration form
 */
function wpturbo_registration_form_shortcode() {
    ob_start();

    // Check if the user is already logged in
    if ( is_user_logged_in() ) {
        echo '<p>You are already registered and logged in.</p>';
    } else {
        ?>
        <form id="registration-form" method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
            <input type="hidden" name="action" value="wpturbo_process_registration">

            <!-- Add your registration form fields here -->
            <p>
                <label for="username">Username:</label>
                <input type="text" name="username" id="username" required>
            </p>

            <p>
                <label for="email">Email:</label>
                <input type="email" name="email" id="email" required>
            </p>

            <p>
                <label for="password">Password:</label>
                <input type="password" name="password" id="password" required>
            </p>

            <p>
                <label for="verification_code">Verification Code:</label>
                <input type="text" name="verification_code" id="verification_code" required>
            </p>

            <p>
                <input type="submit" value="Register">
            </p>
        </form>
        <?php
    }

    return ob_get_clean();
}

/**
 * Process the registration form data
 */
add_action( 'wp_ajax_wpturbo_process_registration', 'wpturbo_process_registration' );
add_action( 'wp_ajax_nopriv_wpturbo_process_registration', 'wpturbo_process_registration' );
function wpturbo_process_registration() {
    // Verify the nonce
    if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'wpturbo_registration_nonce' ) ) {
        wp_send_json_error( 'Invalid nonce' );
    }

    // Get the submitted form data
    $username           = sanitize_text_field( $_POST['username'] );
    $email              = sanitize_email( $_POST['email'] );
    $password           = $_POST['password'];
    $verification_code  = sanitize_text_field( $_POST['verification_code'] );

    // Verify the verification code
    if ( $verification_code !== '1234' ) { // Replace '1234' with your actual verification code
        wp_send_json_error( 'Invalid verification code' );
    }

    // Create the new user
    $user_id = wp_create_user( $username, $password, $email );

    // Check if user creation was successful
    if ( is_wp_error( $user_id ) ) {
        // User creation failed
        wp_send_json_error( $user_id->get_error_message() );
    } else {
        // User creation successful
        wp_set_auth_cookie( $user_id );
        wp_send_json_success( 'User registered successfully' );
    }
}
				

In this code, we have added the necessary JavaScript file 'registration.js' and localized it with the Ajax URL and nonce for secure form submission. The registration form includes a new field for the verification code. When the form is submitted, the wpturbo_process_registration() function is called via Ajax. It verifies the nonce and the verification code before creating the new user. If the verification code is correct, the user is registered and a success message is returned. Otherwise, an error message is returned.

Make sure to replace '1234' in the verification code check with your actual verification code. Additionally, create a new folder named 'js' in the plugin directory and place the 'registration.js' file there. The 'registration.js' file should contain the necessary JavaScript code to handle the Ajax form submission.

Remember to enqueue the JavaScript file using the wpturbo_enqueue_scripts() function. You can customize the form fields and validation as per your requirements.

Register an account to save your snippets or go Pro to get more features.