Authoring Plugins
- Reason: Include information about 1.4 plugin changes
- Parts of this article or section have been written for MyBB version 1.2 and is no longer up to date.
Contents |
[edit] Introduction
Authoring plugins for MyBB is like any other PHP undertaking. It will require some knowledge of PHP/MySQL and a decent knowledge of MyBB.
[edit] Plugin Conventions
[edit] Requirements
These things you MUST do within your plugin code:
- The names of the activation, deactivation, and info functions MUST be the name of your file (minus the .php extension) and _activate, _deactivate, or _info respectively. (e.g. foo_activate if your plugin filename is foo.php)
// The information that shows up on the plugin manager
function pluginname_info()
{
return array(
"name" => "Plugin Name",
"description" => "Plugin Description",
"website" => "Plugin's Website",
"author" => "Your name",
"authorsite" => "Your website",
"version" => "Plugin Version",
);
}
// This function runs when the plugin is activated.
function pluginname_activate()
{
}
// This function runs when the plugin is deactivated.
function pluginname_deactivate()
{
}
- Technically, the activate and deactivate functions are not required to be included if your plugin does not need them, but it's a good idea to include them anyway.
[edit] Good Ideas
- Function Names: Functions in your plugin can be called anything you choose, but it is a good idea to prefix them with the name of your file (minus the extension) in order to avoid clashes with other plugins. (ie. if your filename is foo.php, name your plugin functions: foo_run or foo_bar, foo_foo, etc)
- Rebuild Settings: If your plugin adds new settings to MyBB, it is a good idea to include a function to rebuild settings after adding and removing the settings (this function can be found in the MyBB installation, and is available by default in MyBB 1.2.0).
[edit] The Hook system
[edit] What are the hooks?
[edit] How are the hooks used?
There are 2 types of hooks:
- Hooks without arguments
- Hooks with arguments
First of all we will look at hooks without arguments as this is the most simplest kind. These appear as follows:
$plugins->run_hooks("global_end");
This hook name is global_end. To use this hook in your plugin, add this code somewhere in your plugin file:
$plugins->add_hook("global_end", "foo_functionname");
As can be seen, the hook name is entered in the first parameter, and the function name that you want to be run is entered in the second argument.
Advanced usage follows and can be skipped by average users: 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.
Now we will look at hooks with arguments. These are seen similar to this:
$contents = $plugins->run_hooks("pre_output_page", $contents);
Note two important things for this example plugin hook:
- The $contents variable is an argument for the pre_output_page hook, and thus 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, the hooks work the same as the hooks without arguments.
| Templates/Themes | Authoring - Template Management - Theme Management - User-created |
| Plugins | Authoring - Plugin Hooks - Plugin Management - User-created |
| Translations | Language Management - Download |
| Links | MyBB Mods |