What is a Plugin?
Plugins are similar to snippets in that they are snippets of code that have access to the MODx API - however the big difference is that Plugins are associated with specific System Events. For example, in an average MODx page request, several events happen at certain points within the page parsing process and plugins can be attached to any of these events to fulfill a desired function. That means that when those events "fire," control is transferred to any Plugin "listening" for that event. Once the Plugin's code has executed, control returns to the point after the spot where the System Event was triggered.
Plugins aren't limited to front-end processing, there are many events that are available in the MODx Manager. There is a list of MODx System Events here.
| Any closing PHP tag ?> will be stripped from your plugin code when it is saved. It's unnecessary (and unwanted) because the plugin code will end up inside other PHP code when executed. |
The Event Model
MODx invokes System Events across its code processes to allow you to modify core functionality without hacking the core. These System Events can have any number of Plugins attached to them, and will execute each Plugin in rank according to it's priority.
Handling an Event
In your Plugin, how you handle the output depends on the System Event you are in. For some system events, you return a value from the Plugin. For others, you access the output directly and modify it.
If you need to know which event triggered your plugin (say, for a plugin that listens to more than one event), you can access the Event's name like so:
$eventName = $modx->event->name;
The code for a Plugin listening to more than one event looks like this:
$eventName = $modx->event->name; switch($eventName) { case 'OnWebPageInit': /* do something */ break; case 'OnWebPagePrerender': /* do something else */ break; }
Plugin Examples
Plugins can be used for a variety of different applications, below are a couple of examples:
Message the User:
Description: Send a custom message to the user as they create/edit a page... a custom header.
System Events: onDocFormPrerender
$modx->event->output('Hi there user!');
Custom Validation
Description: Do some custom validation on saving a page resource
System Events: OnBeforeDocFormSave
// Do some logical stuff.... if validation failed: $modx->event->output('Something did not validate!'); return "This goes to the logs";
The trick here is that what you want to message the user has to be passed to the $modx->event->output() function; any text you want to write to the logs can simply be returned by the plugin. If you pass validation, simply return null.
| No HTML Allowed The output you set in $modx->event->output() must not contain any HTML! Use plain text only! |
Word Filter
Description: Filter words from a document before it's displayed on the web
System Events: OnWebPagePrerender
$words = array("snippet", "template"); // words to filter $output = &$modx->resource->_output; // get a reference to the output $output = str_replace($words,"<b>[filtered]</b>",$output);
Page-Not-Found Redirector:
Description: Redirects a user to selected document and sends a message
System Events: OnPageNotFound
System Settings:
- pnf.page: Error Resource ID
- pnf.mailto: Mail To Address
- pnf.mailfrom: Mail From Address
if ($modx->event->name == 'OnPageNotFound') { $errorPage = $modx->getOption('pnf.page'); if (empty($errorPage)) { $modx->sendErrorPage(); } else { $mailto = $modx->getOption('pnf.mailto'); if (!empty($mailto)) { // send a message to a local account $resourceId = $modx->resource->get('id'); $subject = 'Page not found'; $body = 'Someone tried to access document id '.$resourceId; $modx->getService('mail', 'mail.modPHPMailer'); $modx->mail->set(modMail::MAIL_BODY, $body); $modx->mail->set(modMail::MAIL_FROM, '$modx->getOption('pnf.mailfrom')); $modx->mail->set(modMail::MAIL_FROM_NAME, 'MODx'); $modx->mail->set(modMail::MAIL_SENDER, 'MODx'); $modx->mail->set(modMail::MAIL_SUBJECT, $subject); $modx->mail->address('to',$mailto); $modx->mail->setHTML(true); $modx->mail->send(); } $url = $this->makeUrl($scriptProperties['page']); $modx->sendRedirect($url, 1); exit; } }