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]."

$out = &$modx->resource->_output; // get a reference to the resource 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:

/* Get the name of the event currently firing */
$eventName =  $modx->event->name;

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

 

My book, MODX: The Official Guide - Digital Edition is now available here. The paper version of the book is available from Amazon.

If you have the book and would like to download the code, you can find it here.

If you have the book and would like to see the updates and corrections page, you can find it here.

MODX: The Official Guide is 772 pages long and goes far beyond this web site in explaining beginning and advanced MODX techniques. It includes detailed information on:

  • Installing MODX
  • How MODX Works
  • Working with MODX resources and Elements
  • Using Git with MODX
  • Using common MODX add-on components like SPForm, Login, getResources, and FormIt
  • MODX security Permissions
  • Customizing the MODX Manager
  • Using Form Customization
  • Creating Transport Packages
  • MODX and xPDO object methods
  • MODX System Events
  • Using PHP with MODX

Go here for more information about the book.

Thank you for visiting BobsGuides.com

  —  Bob Ray