Custom user group at sign up

Overview

In this example we're going to create a very simple plugin that will take advantage of the "new user" action hook and update user's group based on their gender at sign up. So if we have a man join the site, they will be placed into "regular users" group, and if we have a woman join the site, they will be placed into "paid users" group. Our plugin name will be "CustomUserGroup" which we will use throughout the plugin files. You may download plugin files before we begin or follow the guide below to create them manually.

Plugin core files

To begin, create a new folder called "customusergroup" in "application/plugins" folder. Inside it create a file called "manifest.php" with the following code:

<?php defined('SYSTEM_PATH') || die('No direct script access allowed.');

// "application/plugins/customusergroup/manifest.php"

$params = array(
    'name' => 'Custom user group', // Name of our plugin
    'description' => 'Set user group based on gender at sign up.', // Description
    'author' => 'VLD Interactive Inc.', // Author name
    'website' => 'http://www.vldinteractive.com', // Author website
    'version' => '1.0.1', // Plugin version
    'requirements' => array(
        'system' => '1.3.2' // Our plugin will require software version 1.3.2 or higher to run
    ),
);

Create another file called "install.php" that looks like this:

<?php defined('SYSTEM_PATH') || die('No direct script access allowed.');

// "application/plugins/customusergroup/install.php"

class Plugins_CustomUserGroup_Install extends Plugins
{
    public function __construct($manifest = array())
    {
        parent::__construct($manifest);
    }

    public function update()
    {
        return true;
    }

    public function install()
    {
        return true;
    }

    public function uninstall()
    {
        return true;
    }
}

Since our plugin will not really make any changes in the database, installation and other functions in this class don't need to have any code in them. We still have to have this installation class though, even if it doesn't do anything.

Resource files

Now lets create "install" folder inside our plugin folder and place "hooks.php" file in it with the following code:

<?php defined('SYSTEM_PATH') || die('No direct script access allowed.');

// "application/plugins/customusergroup/install/hooks.php"

$hooks = array(
    'action' => array( // action hooks
        'user/new' => array( // name of the action hook
            'updateUserGroup' => array( // name of the function to run
                'path' => 'customusergroup', // path to the hook file
                'object' => 'myfunctions' // name of the hook file
            )
        ),
    )
);

That's it for our plugin core files setup. Go to the control panel and under "system - plugins" you will see the "Custom user group" plugin. Click on "install" link to install it. What this does is populates the database with all the information we have entered in the files above.

Functionality

Even though the plugin is installed, it won't do anything yet so lets create our action hook file. In the "application/hooks/customusergroup" create a file called "myfunctions.php" with this code in it:

<?php defined('SYSTEM_PATH') || die('No direct script access allowed.');

// "application/hooks/customusergroup/myfunctions.php"

// "CustomUserGroup" is the folder name
// "MyFunctions" is the file name
class CustomUserGroup_MyFunctions_Hook extends Hook
{
    public function updateUserGroup($userID)
    {
        // We take advantage of the session::item('profile', 'signup') which stores
        // user profile values at signup. If you add users manually in the control
        // panel, those values won't exist so this code will run only at signup.
        // We check if "gender" profile field is equal to "2" which is the ID of the
        // "woman" item.
        if ( session::item('profile', 'signup', 'data_gender') == 2 )
        {
            // If this user is a woman, we will set their group ID to "2" which is the
            // "paid users" group.
            $this->db->update('users', array('group_id' => 2), array('user_id' => $userID), 1);
        }

        return true;
    }
}

That's it. Whenever someone signs up, system will check if they are a women and assign them to "paid users" group. Of course you may modify functionality of this plugin and take it much further than this.