Is there any way to give each user an additional role when registering with the User-Registrauoin add-on?
Unfortunately this does not work:
Is there any way to give each user an additional role when registering with the User-Registrauoin add-on?
Unfortunately this does not work:
Hi Fränk. If that code does not work for you, I recommend opening a support ticket:
$user is undefined in this code.
simply add prior to $user being used
$user = new WP_User( $user_id );
This might do the job:
// Run this code only for form id 1
add_action( 'gform_user_updated_1', 'add_additional_role1', 10, 4 );
function add_additional_role1( $user_id, $feed, $entry, $user_pass ) {
// Role name ID to add
$role_to_add = 'role_name';
// Get current user object
$user_obj = new WP_User( $user_id );
// Add the role above to existing role(s) for the user
$user_obj->add_role( $role_to_add );
}
I forgot to mention that you need to obviously create said role with its capabilities first (if it’s a non-standard role), so the snippet knows what role “role_name” (change to your role name) refers to.
Oh, and you need to use the “Update User” function of the User Registration add-on in order to add the role to the user’s existing role.
Thanks for that catch Michael. I updated the original code example to add:
$user = new WP_User( $user_id );