John Coles
Member
Hello all!
I am trying to write a PHP script that will add and remove a secondary user group to a user. The adding seems to work reliably but I can not get removing to work. I also can't find an example of removing a specific group ID.
Here's the code I have (mainly taken from https://xenforo.com/community/threads/change-user-group-via-php.42233/#post-1016279):
Adding the group
Removing the group
I'd appreciate any pointers in the right direction. I had planned to do this in SQL directly but am aware that using the Xenforo API is a far far better way to do it!
I am trying to write a PHP script that will add and remove a secondary user group to a user. The adding seems to work reliably but I can not get removing to work. I also can't find an example of removing a specific group ID.
Here's the code I have (mainly taken from https://xenforo.com/community/threads/change-user-group-via-php.42233/#post-1016279):
Adding the group
<?php
$startTime = microtime(true);
$fileDir = '/srv/www/myforum.net/www';
require($fileDir . '/library/XenForo/Autoloader.php');
XenForo_Autoloader::getInstance()->setupAutoloader($fileDir . '/library');
XenForo_Application::initialize($fileDir . '/library', $fileDir);
$user_id = 1337;
$upgrade_id = "group_upgrade_".date(U);
$userModel = XenForo_Model::create('XenForo_Model_User');
$userModel->addUserGroupChange($user_id, $upgrade_id, 22);
echo "Added -> ".$upgrade_id;
?>
Removing the group
<?php
$startTime = microtime(true);
$fileDir = '/srv/www/myforum.net/www';
require($fileDir . '/library/XenForo/Autoloader.php');
XenForo_Autoloader::getInstance()->setupAutoloader($fileDir . '/library');
XenForo_Application::initialize($fileDir . '/library', $fileDir);
$user_id = 1337;
$downgrade_id = "group_remove_".date(U);
$userModel = XenForo_Model::create('XenForo_Model_User');
$userModel->removeUserGroupChange($user_id, $downgrade_id);
echo "Removed -> ".$downgrade_id;
?>
I'd appreciate any pointers in the right direction. I had planned to do this in SQL directly but am aware that using the Xenforo API is a far far better way to do it!