- Write user plugin which will have the below file structure
- plugins/user/plugins_name/
- plg_add_group.php
- plg_add_group.xml
plg_add_group.xml
- Write plugin manifest code here you can refere below link for the same. https://docs.joomla.org/J3.x:Creating_a_Plugin_for_Joomla
plg_add_group.php : Write below php code in this file.
<?php
defined('_JEXEC') or die('Unauthorized Access');
class PlgUserTest extends JPlugin
{
public function onUserAfterSave($data, $isNew, $result, $error)
{
if($data['com_fields']['do-you-want-to-become-student']==1)
{
//Note - This code in your case need little bit modification as here I not removed current user group which is register in your case, So if you wanted to add only student group you should be removing all other groups before adding student group.
jimport('joomla.user.helper');
JUserHelper::addUserToGroup($data['id'], 3);
// 3 Should be your student user group id
}
return true;
}
}
?>
Joomla gives user type of plugin which have onUserBeforeSave() trigger, but this trigger never sends updated/injected data to save method of user hence we need to use onUserAfterSave() trigger to update group of user after it saves, If you see the code we have called addGroup method of joomla user helper to add group if User choose Yes in registration form 'Do you want to become Student field'.
$data['com_fields']['do-you-want-to-become-student']
This line is for getting your Yes/No field value, so the key need to update according to your fields name.