How to Assign User Role Based on WordPress Registration Forms
Are you faced with the challenge of creating custom registration forms for various user roles on your WordPress website? Perhaps you operate a membership site, an online marketplace, or a community forum where different user types require distinct access and privileges.
In this article, we’ll explore two methods to assign different user roles based on the WordPress registration forms they signed up from.
Whether you’re a seasoned WordPress developer or a novice website owner, this guide will explain how to assign different user roles per registration form in your WordPress website.
Table of Contents
Assigning Different Roles Based on WordPress Registration Forms
The ProfilePress plugin is the perfect solution for assigning different roles to WordPress users based on the registration form they register from. ProfilePress is ideal for creating custom WordPress registration forms.
Install and Activate ProfilePress Plugin
To get started, you need the ProfilePress plugin installed and activated on your website. You can download a copy after making a purchase.
Once you have downloaded the plugin, log in to your WordPress admin dashboard and navigate to the Plugins page. Click the “Add New” button, then select “Upload Plugin.”
Next, click “Choose File” to select the previously downloaded plugin ZIP file. Click “Install Now” and then activate the plugin.
Creating a WordPress User Registration Form
Navigate to the ProfilePress dashboard, click “Forms & Profiles,” and select “Add New.”
Next, choose a builder type. For this article, we will opt for the “Drag & Drop Builder.”
Click the ‘Get Started’ button under the Drag & Drop Builder. Next, you will see a field to enter the name for your form. Enter a name – you can call it ‘Registration Form.’ Then, change the form type to ‘Registration’ by clicking the ‘Registration’ tab.
Now, you should see various pre-made registration form templates that you can choose from, along with the “Do It Yourself” option. Click on the “Do It Yourself” option to begin building a registration form from scratch.
On the form builder, numerous form fields are available for customization. Additionally, at the top of the fields, you’ll see our form shortcode and a live preview button, allowing you to preview any changes made to your form.
We can drag and drop fields from the right sidebar to customize our registration forms.
Drag and drop the Confirm Password field into the form builder area and position it directly below the Password field. This addition enables users to input their password twice, ensuring they accurately confirm it matches before registering.
You can delete any unwanted fields from the form builder by hovering your mouse over a field and clicking the Bin icon.
Furthermore, click on the Gear icon to adjust the settings for any specific form field. This action will prompt a popup window displaying the available settings for that particular field. You’ll find three sections within this popup – General, Style, and Settings. Each section offers various elements that enable you to modify the field’s display and behavior according to your preferences.
Assigning User Roles to the WordPress Registration Forms
After customizing the WordPress registration form, scroll down to the ‘Registration Settings’ section within the form settings. Here, you can choose the default user role for users who register using this form.
Select the desired role from the dropdown menu.
Once you are done configuring the form settings, click on the ‘Save Changes’ button.
Creating a WordPress Registration Page
The final step is to add the registration form to a WordPress page using the provided shortcode. Users who register through this form will automatically be assigned the specified user role.
To do this, copy your form shortcode, shown at the top of the form builder page.
Next, navigate to Pages > Add New, and enter the page title – Register. If you are using the WordPress block editor, click on the plus icon, select the shortcode block, and paste the copied form shortcode into this block.
If you use the WordPress classic editor, paste the shortcode into the text editor. Then click ‘Publish’ to make your new WordPress user registration page live.
Alternative Implementation via Code
Before Profilepress version 1.9, defining a user role that registering users will be assigned — aside from the one set in WordPress general settings — was only possible via pp_after_registration
action hook like so:
add_action( 'ppress_after_registration', 'pp_insert_user_role', 10, 3 );
function pp_insert_user_role( $form_id, $user_data, $user_id ) {
$a = get_user_by( 'id', $user_id );
// clear existing role if exist.
$a->set_role( '' );
// add the roles
$a->add_role( 'student' );
}
We do not recommend the code approach because it is not flexible enough compared to the built-in user role assignment per registration form in ProfilePress.
We hope this article helped you learn how to assign different user roles based on the WordPress registration form they signed up from.
If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.