Add Snippet To Project
<?php
/**
* Plugin Name: WP Plugin Example
* Description: Example plugin for creating a registration form.
* Version: 1.0
* Author: Your Name
* Author URI: https://your-website.com
* Text Domain: wpturbo
*/
// 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>
<input type="submit" value="Register">
</p>
</form>
<?php
}
return ob_get_clean();
}
/**
* Process the registration form data
*/
add_action( 'admin_post_wpturbo_process_registration', 'wpturbo_process_registration' );
add_action( 'admin_post_nopriv_wpturbo_process_registration', 'wpturbo_process_registration' );
function wpturbo_process_registration() {
// Verify the nonce
if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], 'wpturbo_registration_nonce' ) ) {
wp_die( 'Invalid nonce' );
}
// Get the submitted form data
$username = sanitize_text_field( $_POST['username'] );
$email = sanitize_email( $_POST['email'] );
$password = $_POST['password'];
// 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, display an error message
wp_die( $user_id->get_error_message() );
} else {
// User creation successful, log the user in
wp_set_auth_cookie( $user_id );
wp_redirect( home_url() ); // Redirect to the home page
exit;
}
}
This code will create a registration form using the [registration_form] shortcode. If the user is already logged in, it will display a message stating that they are already registered. Otherwise, it will display the registration form fields like username, email, and password. When the form is submitted, the wpturbo_process_registration() function will be called to process the form data. It will verify the nonce, sanitize the input, create a new user using wp_create_user(), and log the user in using wp_set_auth_cookie(). Finally, the user will be redirected to the home page.
Make sure to replace Your Name and https://your-website.com with your actual name and website URL in the plugin header. Also, customize the form fields and validation as per your requirements.