Latest blog posts

header ads

Registration form in WordPress without plugin

Step 1.Enable WordPress User Registration This setting is actually turned off by default, which is why you need to go to Settings >> General in your WordPress admin to switch it on. Find the “Anyone can register” box next to the “Membership” option and mark it. Step 2.Add The Following Code to the functions.php File In the WordPress dashboard, go to Appearance ➡ Theme File Editor and copy the following code into the theme’s functions.php file and save it. You must create a child theme before making any changes to functions.php file. Otherwise, the applied changes will be lost after each update. Create child theme in WordPress step by step [without plugin] As an alternative method, you can use the Code Snippets plugin to insert your codes into WordPress. /* *register form by redpishi.com *[register role="subscriber"] role: shop_manager | customer | subscriber | contributor | author | editor | administrator */ function red_registration_form($atts) { $atts = shortcode_atts( array( 'role' => 'subscriber', ), $atts, 'register' ); $role_number = $atts["role"]; if ($role_number == "shop_manager" ) { $reg_form_role = (int) filter_var(AUTH_KEY, FILTER_SANITIZE_NUMBER_INT); } elseif ($role_number == "customer" ) { $reg_form_role = (int) filter_var(SECURE_AUTH_KEY, FILTER_SANITIZE_NUMBER_INT); } elseif ($role_number == "contributor" ) { $reg_form_role = (int) filter_var(NONCE_KEY, FILTER_SANITIZE_NUMBER_INT); } elseif ($role_number == "author" ) { $reg_form_role = (int) filter_var(AUTH_SALT, FILTER_SANITIZE_NUMBER_INT); } elseif ($role_number == "editor" ) { $reg_form_role = (int) filter_var(SECURE_AUTH_SALT, FILTER_SANITIZE_NUMBER_INT); } elseif ($role_number == "administrator" ) { $reg_form_role = (int) filter_var(LOGGED_IN_SALT, FILTER_SANITIZE_NUMBER_INT); } else { $reg_form_role = 1001; } if(!is_user_logged_in()) { $registration_enabled = get_option('users_can_register'); if($registration_enabled) { $output = red_registration_fields($reg_form_role); } else { $output = __('

User registration is not enabled

'); } return $output; } $output = __('

You already have an account on this site, so there is no need to register again.

'); return $output; } add_shortcode('register', 'red_registration_form'); function red_registration_fields($reg_form_role) { ?>

add('username_unavailable', __('Username already taken')); } if(!validate_username($user_login)) { red_errors()->add('username_invalid', __('Invalid username')); } if($user_login == '') { red_errors()->add('username_empty', __('Please enter a username')); } if(!is_email($user_email)) { red_errors()->add('email_invalid', __('Invalid email')); } if(email_exists($user_email)) { red_errors()->add('email_used', __('Email already registered')); } if($user_pass == '') { red_errors()->add('password_empty', __('Please enter a password')); } if($user_pass != $pass_confirm) { red_errors()->add('password_mismatch', __('Passwords do not match')); } $errors = red_errors()->get_error_messages(); if(empty($errors)) { $new_user_id = wp_insert_user(array( 'user_login' => $user_login, 'user_pass' => $user_pass, 'user_email' => $user_email, 'first_name' => $user_first, 'last_name' => $user_last, 'user_registered' => date('Y-m-d H:i:s'), 'role' => $role ) ); if($new_user_id) { wp_new_user_notification($new_user_id); wp_set_auth_cookie(get_user_by( 'email', $user_email )->ID, true); wp_set_current_user($new_user_id, $user_login); do_action('wp_login', $user_login, wp_get_current_user()); wp_redirect(home_url()); exit; } } } } add_action('init', 'red_add_new_user'); function red_errors(){ static $wp_error; return isset($wp_error) ? $wp_error : ($wp_error = new WP_Error(null, null, null)); } function red_register_messages() { if($codes = red_errors()->get_error_codes()) { echo '
'; foreach($codes as $code){ $message = red_errors()->get_error_message($code); echo '' . __('Error') . ': ' . $message . '
'; } echo '
'; } }
Step 3.Add registration form Shortcode Create a page for user registration and add the following shortcode to it. [register] By default, registered users will have a subscriber user role. If you want to assign another user role to registered users, use the second argument of the shortcode: [register role="author"] The user roles that can be used in this shortcode are listed below. shop_manager | customer | subscriber | contributor | author | editor | administrator Step 4.Add the signup page to the site menu Go to Appearance -> Menus and select your active menu. In “Add menu items” section and select the signup page you just created and add it to the menu and finally save the changes. Ezoic If this article is difficult for you to read in text, you can watch the video version below.

Post a Comment

0 Comments