How and when plugins and their init functions called in ELGG

Plugins are responsible for the rich features and flexibility of Elgg. The order of plugins matters and wrongly ordered plugins could lead to blank screens and other mis-functionalities. Following are a few things every Elgg developer and Elgg admin should know about when and how plugins are called.

When a user clicks on a page, Elgg calls the start.php file of every plugin in the order the plugins are listed in the Tools Administration menu. plugin_init() functions are registered with the register event handler function.

register_elgg_event_handler('init','system','myplugin_init',123);

If you need the start.php of your plugin to execute before any other start.php, order you plugin at the top and for precaution, call your init function directly instead of registering it.

myplugin_init();

instead of

register_elgg_event_handler('init','system','myplugin',123);

This is bad programming practice but it is necessary when writing a plugin to debug or develop plugins. Elgg Developer Tools (elgg_dev_tools) uses this method.

If you need you init function to be the very first init function to call, the Elgg way is to use the following code

register_elgg_event_handler('plugins_boot','system','myplugin);

The fourth, optional integer argument can be used to define the order the plugins are called. If you define a number, the init function would be called in the order of incrementing numbers. If you number your plugins between 1 and 499 inclusive, they would be called before init function which do not specify a number. If you number your plugins between 600 and 9999 inclusive, they would be called after the plugins which did not specify a number. Numbers between 500 and 599 inclusive would place the plugins in between the non-numbered plugins proportional to their numeric weight.

init() functions of plugins are NOT called in the order they are listed in the Tools Adminstration page. You might need to remove numbers from existing plugins register_elgg_event_handler() functions to get some plugins to work together.