Step 6. Adding Hooks To Your Plugin

Hooks are a fundamental concept used by almost all plugins allowing you to trigger your plugin's code when a member performs an action in Zenbership. For example, you may want to run some plugin code after a member registers in Zenbership.

Plugin hooks run exactly like other hooks and therefore should follow the same concepts laid out in the "Hooks" section of this documentation.

Calling a Plugin's Custom Classes Within a Hook

https://github.com/castlamp/zenbership_plugins/blob/master/mybb/hooks/register.php

Consider this example, taken from the MyBB plugin's member_create hook:

<?php
// We load the MyBB plugin object
$plugin = new plugin('mybb');

// We load the plugin's custom "bbsync" class.
// https://github.com/castlamp/zenbership_plugins/tree/master/mybb/functions
$loaded = $plugin->load('bbsync');

// We grab a plugin option
$contentId = $plugin->option('content_id');

// We loop through the content that the registering
// member was granted access to when he/she registered.
foreach ($data['content'] as $item) {
    // We determine whether the content ID option matches
    // any of the content to which the user was granted access.
    if ($item['id'] == $contentId) {
        // If found, we connect to the MyBB database and
        // create the user.
        $plugin->connectLocal();
        $create = $loaded->create(array(
            'username' => $data['data']['username'],
            'email' => $data['data']['email'],
        ));

        // Finally we connect back to the Zenbership database.
        $plugin->connectZen();
        break;
    }
}

In the above example, we are trying to match the plugin's content_id option against the list of content the member was granted access to when he/she registered. This option was created during the installation process for the plugin (see the code example)

In simpler words, assume we are selling a product that grants access to a piece of content that we created within the Zenbership administrative dashboard. Now let's assume that we only want users who have access to that content to have access to the forum. The content_id option holds that content's database ID and uses that to match whether Zenbership gave the member access to it when he/she registered.

The $data['content'] array comes from the hook itself; you can learn more about the variable available within a hook here.

Last modified 4 months ago.