The code snippet below demonstrates how to assign a user role to a user when they submit a form using the WP Forms plugin in WordPress. This can be useful in scenarios where you want to automatically assign a specific role to users based on the form they submit.
/**
* Assign user role on WP Forms submission
*/
function wpsnippets_assign_user_role_on_form_submission( $entry, $form_data ) {
// Get the submitted form data
$form_fields = $form_data['fields'];
// Get the user ID from the form entry
$user_id = $entry['created_by'];
// Set the default role to assign
$role_to_assign = 'subscriber';
// Check if a specific field value is submitted
if ( isset( $form_fields['user_role'] ) && ! empty( $form_fields['user_role']['value'] ) ) {
// Get the selected role from the form submission
$selected_role = $form_fields['user_role']['value'];
// Validate the selected role
if ( in_array( $selected_role, array( 'subscriber', 'contributor', 'author', 'editor' ) ) ) {
$role_to_assign = $selected_role;
}
}
// Assign the role to the user
$user = new WP_User( $user_id );
$user->set_role( $role_to_assign );
}
add_action( 'wpforms_process_complete', 'wpsnippets_assign_user_role_on_form_submission', 10, 2 );
In this code snippet, we use the wpforms_process_complete
action hook to trigger the function wpsnippets_assign_user_role_on_form_submission
when a form submission is completed.
Inside the function, we first retrieve the submitted form data and the user ID associated with the form entry. We then set the default role to assign as ‘subscriber’.
Next, we check if a specific field value (in this case, a field with the name ‘user_role’) is submitted and not empty. If it is, we validate the selected role against an array of allowed roles (‘subscriber’, ‘contributor’, ‘author’, ‘editor’). If the selected role is valid, we update the $role_to_assign
variable.
Finally, we use the WP_User
class to retrieve the user object based on the user ID and assign the role using the set_role()
method.
By customizing this code snippet and modifying the role assignment logic, you can assign different user roles based on the form submission.
Examples
Example 1: Assigning User Role to WP Forms Submitter
This use case demonstrates how to assign a specific user role to a user when they submit a form created with WP Forms plugin.
function wpsnippets_assign_user_role_on_form_submission( $entry, $form_id ) {
$user_id = $entry['created_by'];
$role = 'subscriber';
if ( $user_id && $role ) {
$user = new WP_User( $user_id );
$user->set_role( $role );
}
}
add_action( 'wpforms_process_complete', 'wpsnippets_assign_user_role_on_form_submission', 10, 2 );
This code example uses the wpforms_process_complete
action hook to trigger the function wpsnippets_assign_user_role_on_form_submission
when a form submission is completed. It retrieves the user ID from the form entry and assigns the specified user role (in this case, ‘subscriber’) to the user using the set_role()
method of the WP_User
class.
Example 2: Assigning Custom User Role on Form Submission
This use case demonstrates how to assign a custom user role to a user when they submit a form created with WP Forms plugin.
function wpsnippets_assign_custom_user_role_on_form_submission( $entry, $form_id ) {
$user_id = $entry['created_by'];
$role = 'custom_role';
if ( $user_id && $role ) {
$user = new WP_User( $user_id );
$user->set_role( $role );
}
}
add_action( 'wpforms_process_complete', 'wpsnippets_assign_custom_user_role_on_form_submission', 10, 2 );
This code example is similar to the previous one, but it assigns a custom user role (in this case, ‘custom_role’) to the user instead of a predefined role. You can replace 'custom_role'
with the desired role slug.
Example 3: Assigning Multiple User Roles on Form Submission
This use case demonstrates how to assign multiple user roles to a user when they submit a form created with WP Forms plugin.
function wpsnippets_assign_multiple_user_roles_on_form_submission( $entry, $form_id ) {
$user_id = $entry['created_by'];
$roles = array( 'subscriber', 'contributor' );
if ( $user_id && ! empty( $roles ) ) {
$user = new WP_User( $user_id );
foreach ( $roles as $role ) {
$user->add_role( $role );
}
}
}
add_action( 'wpforms_process_complete', 'wpsnippets_assign_multiple_user_roles_on_form_submission', 10, 2 );
This code example assigns multiple user roles (in this case, ‘subscriber’ and ‘contributor’) to the user. It uses the add_role()
method within a loop to add each role to the user. You can modify the $roles
array to include the desired roles.