MODx Plugins

Plugins let you step in and alter things during the operation of the MODx engine. All plugins are tied to MODx System Events. When a system event "fires," all the plugins attached to that event are executed.

System Events

At various points during the operation of MODx, System Events are invoked. The System Event names always begin with "On" (e.g. OnWebPagePrerender, OnDocFormSave, OnUserFormSave, etc.). Look here for a complete list.

Plugins are assigned (usually when created) to "listen" for one or more System Events. When a System Event fires, the code of any plugins tied to that event executes.

Plugin Example

This simple plugin, when linked to the OnWebPagePrerender event, will replace all occurrences of "crap" with "[Expletive Deleted]."

$e = & $modx->Event; // get a reference to the event
$out = &$modx->documentOutput; // get a reference of the output
$out = str_replace('crap',' '[expletive deleted]',$out);
return; 

Sometimes, a plugin will be tied to more than one System Event and different code in the plugin will be executed depending on which event has fired. In that case, the plugin must identify the name of the System Event, something like this:

$e = & $modx->Event; // get a reference to the event
$eventName =  $e->name;

switch ($eventName) {
    case 'OnWebPagePrerender':
       // do something
       break;
    case 'OnWebPageInit':
       // do something else
       break;
}
return;

Thank you for visiting BobsGuides.com

  —  Bob Ray