Assign a new Joomla! user group to the logged-in user after form submission

If you have a form that is accessible only to logged-in users and want to automatically add them to a new Joomla! User Group upon submission, you can achieve this by adding a a PHP script within Script called after form has been processed area located under backend > Components > RSForm!Pro > Manage Forms > your form > Properties > PHP Scripts:

// Set the User Group ID here
$groupID = 10;
// Get the current user's ID
$userID = str_replace($replace, $with, '{global:userid}');

if ($userID) {
    // Load the Joomla! user object
    $user = Joomla\CMS\Factory::getUser($userID);

    if ($user->id) {
        // Get current user groups
        $groups = $user->get('groups');

        // Add the user group ID (10) if not already assigned
        if (!in_array($groupID, $groups)) {
            $groups[] = $groupID;

            // Assign the new user groups
            $user->set('groups', $groups);
            $user->save();
        }
    }
}

Replace 10 with the ID of the Joomla! User Group to which you want users to be assigned.

 

Was this article helpful?

Yes No
Sorry about that