What is a hook?

Action hooks are essentially placeholders. Wherever an action hook is placed, it will execute any code that has been “hooked” to it. It allows you to run your own custom code without having to edit software files, thus making development and updates easier.

Hook types

There are 2 types of hooks:

  • Action hooks - they are executed when certain action takes place (such as user signs up, new picture uploaded, etc).
  • Filter hooks - they are executed when certain data is processed by the software which you may then alter using your own code.

Creating hooks

In order to function, a hook must do the following:

  • Reside in "application/hooks" folder (or a sub-folder).
  • The class name must map to the file name (with / replaced with _) and each word is capitalized.
  • Must have the Hook class as a (grand)parent.
  • Must have a related record in "core_hooks" table in the database.

Some examples of hook names and locations:

// application/hooks/billing/billing.php
class Billing_Billing_Hook extends Hook {

// application/hooks/users/myfunctions.php
class Users_MyFunctions_Hook extends Hook {

// application/hooks/myfunctions.php
class MyFunctions_Hook extends Hook {

In order for the system to run your hooks, you must insert a related record into the "core_hooks" table in the database. Normally this is done automatically by the software when you create and install a plugin.

Example hooks

Typical hook would look like this:

class Users_MyFunctions_Hook extends Hook
	// action hook
	public function newUser($userID)
		// do something when new user joins the site

		return true;