Hooks

Hooks allow executing code and modifying data at predefined places by registering them with callback functions.

Overview

Development & Support
Customizations are not included with support. We provide this information to make it easier for developers to extend the functionality. From time to time we may have some availability for custom work. Get in touch to see if there's an opportunity to work together.

There are two types of hooks available:

  • Filters

    Receive, modify and return data back to the calling hook. Can be used to modify the page title and metatags, override specific user permissions, add or remove CSS and scripts from the page, etc.

  • Actions

    Receive data, execute code, and return nothing. Can be used to run code at specific points in the request and echo output at pre-defined places in templates.

Find can find a list of available hooks in the Hooks Directory

Getting Started

To use a hook, it's necessary to register a hook callback function using the hook name. The callback function is executed by the hook system and receives data, in the case of filters, along with parameters. Filter callback functions must always return the data back to the originating hook.

To get started, create the filter_functions.php file in overrides:

Joomla

templates/
`-- jreviews_overrides/
    `-- filters/
        `-- filter_functions.php

WordPress

jreviews_overrides/
`-- filters/
     `-- filter_functions.php

The file needs to begin with the following code:

<?php
defined('MVC_FRAMEWORK') or die;

Now you can register all your hook callbacks directly in this file, or organize them into different files as explained at the end of this article.

To register a Filter Hook you can use function closures:

Clickfwd\Hook\Filter::add('filter_name', function($data, $params)
{
  return $data;
}, $priority = 10);

Or, create a named function, and then add the function name to the callback parameter:

function filter_callback($data, $params = [])
{
   // Modify the $data and return it
   return $data;
}

Clickfwd\Hook\Filter::add('filter_name', 'filter_callback', $priority = 10);

Named functions can come in handy, when you can use the same callback function for several hooks.

Registering an Action Hook is similar, but nothing is returned from inside the function.

Clickfwd\Hook\Action::add('action_name', function($params)
{
  // Execute some action or echo some output
}, $priority = 10);

Examples & Tutorials

You can find examples to use in the Hooks Directory, and also within the JReviews and Add-ons source code. In JReviews you can look in the following directories.

Joomla

/components/com_jreviews/jreviews/filters/
/components/com_jreviews/jreviews/plugins/

WordPress

/wp-content/plulgins/jreviews/jreviews/filters/
/wp-content/plulgins/jreviews/jreviews/plugins/

You can also find development tutorials in our blog:

The TrustedUsers Add-ons is also a fine example because it is developed entirely using hooks.

Organizing Hooks

If you plan on using a lot of hooks on your site, one way to organizing them is to create a file per hook, and then include that file from within filter_functions.php.

`-- filters/
     |-- filter_functions.php
     |-- hook_one.php
     |-- hook_two.php
     `-- hook_three.php

In filter_functions.php

<?php
defined('MVC_FRAMEWORK') or die;

require_once 'hook_one.php';

require_once 'hook_two.php';

require_once 'hook_three.php';