MyBB Documentation

Plugins:The Hook System

The 1.6 Documentation is no longer maintained and some information may be outdated.

The MyBB 1.6 series reached end of life on October 1, 2015.

This means there will be no more security or maintenance releases for these series and forums running these versions of MyBB may be at risk of unfixed security issues. All administrators are strongly encouraged to upgrade their forums to the latest release of MyBB as soon as possible.

MyBB has places in its code where your plugin can "hook" into. There are two types of hooks used in MyBB: hooks without arguments, and hooks with arguments.

First, hooks without arguments are the most simplest kind. They appear as:


This hook name is "global_end" (found in global.php). To use this hook in your plugin, add this code at the top of your plugin, before the _info function and after the security check.

	$plugins->add_hook("global_end", "foo_functionname");

The general structure is the hook name, and then the function name you want to run when it comes to that point in the code.

For example, when global_end is run during the loading of a page, the foo_functionname function is also run at the same time. This means you can insert your own code to do pretty much whatever you like - hook functions can be made up of any PHP code.

Advanced Usage Most users can skip this part. The add_hook method can also accept two optional parameters: priority and file.

	$plugins->add_hook("global_end", "foo_functionname", 5, "anotherfile.php");

The priority takes into effect if there are two or more functions that are associated with a hook. A lesser value in the priority parameter will mean the function will be executed before the other function(s) with a higher value priority (for example, a priority "0" will execute before "10"). The default priority is 10. Usually you will not need to use this parameter unless you have two plugins that conflict with each other on one hook.

The filename in the file parameter will be "included-once" when the hook is run, before the function is executed.

Hooks with arguments are seen like this:

	$contents = $plugins->run_hooks("pre_output_page", $contents);

In this example, the $contents variable is an argument for the pre_output_page hook, and so the function declaration for this hook must include this argument like this:

	function foo_functionname ($contents)

The return value of the hooks is placed back into $contents. This means at the end of your plugin function you must return $contents back, or else $contents will be empty. Otherwise, these hooks work the same as the hooks without arguments.

However, one must use caution and proper security practices when using hooks that contain arguments. This is due to the ability for other plugins to alter the arguments prior to your plugin being run. Plugin authors should never rely on the argument contents being safe before using them, type casting and escaping variables before using them should be a standard practice.

Edit this page on GitHub