Sometimes you need a custom registration form. A registration form where more fields can be added and where you can assign a specific role based on user input.
Plugin Registration
Better to put the to be added code in a mu-plugins plugin instead of the theme’s function.php. So let’s start with that.
<?php /* Plugin Name: Custom Registration Fields Plugin URI:https://wpvilla.in Description: Version: 0.1 Author: WP Villain Author URI: https://wpvilla.in License: GPLv2 or later License URI: http://www.gnu.org/licenses/gpl-2.0.html */
Custom Registration Form
Now you need to add a field to the registration form. And perhaps even more then one, but we start with one:
add_action( "register_form", function() { $html = '<p>'.__('Do you want to buy or sell?<br>Choose your role').'</p>'; $html .= '<p>'; $html .= '<label for="_customer"><input type="radio" name="user_role" id="_customer" value="customer"/>'.__('Customer').'</label><br>'; $html .= '<label for="_vendor"><input type="radio" name="user_role" id="_business" value="_vendor"/>'.__('Business').'</label>'; $html .= '</p>'; echo $html; } );
Props Kudratullah @ https://wordpress.stackexchange.com/a/253129/12260
Error Checking
We also need to make sure the choices needed are made so they do need to check for a role
add_filter( 'registration_errors', 'imwz_registration_errors', 10, 3 ); function imwz_registration_errors( $errors, $sanitized_user_login, $user_email ) { if ( empty( $_POST['YourFieldName'] ) ) { $errors->add( 'role_choice_error', __( '<strong>ERROR</strong>: Please pick a role.', 'imwz' ) ); } return $errors; }
The filtered WP_Error object may, for example, contain errors for an invalid or existing username or email address. A WP_Error object should always returned, but may or may not contain errors. https://developer.wordpress.org/reference/hooks/registration_errors/
NB Sanitizing is missing here. Will be added as soon as possible
Action to Set Role on Registration
Then we need another action hook to make sure that on registration the proper user role is added for the user based on choice made.
add_action( "user_register", function( $user_id ) { $allowed = array( 'customer', 'business' ); if( isset( $_POST['YourFieldName'] ) && in_array( $_POST['YourFieldName'], $allowed ) ){ $user = new WP_User( $user_id ); $user->set_role($_POST['YourFieldName']); } } );
Action ‘user_register‘ takes place after a user is inserted into the database.
NB If you are using ACF to add the role on registration you would have something like this when you would be using radio buttons and want to check for one of the two choices made:
add_action( "user_register", function( $user_id ) { $selected_radio = $_POST['field_5e44e2456a4a9']; $user = new WP_User( $user_id ); if( $selected_radio == 'bedrijf') { $user->set_role('bedrijf'); } } );
Custom User Profile Fields
We will also need to add the fields to the backend user profile. Code below is still more of a concept.
/** * Back end registration */ add_action( 'user_new_form', 'imwz_admin_registration_form' ); function imwz_admin_registration_form( $operation ) { if ( 'add-new-user' !== $operation ) { // $operation may also be 'add-existing-user' return; } $year = ! empty( $_POST['customer_type'] ) ? intval( $_POST['customer_type'] ) : ''; ?> <h3><?php esc_html_e( 'Personal Information', 'imwz' ); ?></h3> <table class="form-table"> <tr> <th><label for="year_of_birth"><?php esc_html_e( 'Customer Type', 'imwz' ); ?></label> <span class="description"><?php esc_html_e( '(required)', 'imwz' ); ?></span></th> <td> <input type="radio" name ="user_role" value = "Business" /> <?php __('Business').'</label><br>'; ?> </td> </tr> </table> <?php }
More will be added soon here
ACF & Registration Form
Another option would be to use ACF to add all the forms needed. Here is a basic hallo field generated in ACF that is added to the form:
if( function_exists('acf_add_local_field_group') ): acf_add_local_field_group(array( 'key' => 'group_5e42248c978d2', 'title' => 'Registration Form', 'fields' => array( array( 'key' => 'field_5e422541ddb04', 'label' => 'Hello', 'name' => 'hello', 'type' => 'text', 'instructions' => '', 'required' => 0, 'conditional_logic' => 0, 'wrapper' => array( 'width' => '', 'class' => '', 'id' => '', ), 'default_value' => '', 'placeholder' => '', 'prepend' => '', 'append' => '', 'maxlength' => '', ), ), 'location' => array( array( array( 'param' => 'user_form', 'operator' => '==', 'value' => 'register', ), ), ), 'menu_order' => 0, 'position' => 'normal', 'style' => 'seamless', 'label_placement' => 'top', 'instruction_placement' => 'label', 'hide_on_screen' => '', 'active' => true, 'description' => '', )); endif;
And you can use it to add radiobuttons as well as conditionals. Only thing that may need tweaking is adding the data for user role to the user registration role. But for that we can still use the add_action hook mentioned earlier.
NB https://developer.wordpress.org/reference/hooks/user_new_form/
NB https://www.wpbeginner.com/plugins/how-to-create-a-custom-user-registration-form-in-wordpress/ , https://www.cssigniter.com/how-to-add-custom-fields-to-the-wordpress-registration-form/ and https://gist.github.com/jasperf/7303deb46c602a357fa4a3b671067fc9
One Response