StripWords Custom Output Modifier

Creating a custom output modifier

By Bob Ray  |  June 18, 2024  |  4 min read
StripWords Custom Output Modifier

MODX Revolution has a wide variety of output modifiers. There's a great list of them here. But sometimes there isn't a modifier to do exactly what you want. When MODX sees the output modifier token (:) followed by a word it doesn't recognize as one of its internal output modifiers, it looks for a snippet with that name. It if finds it, it will execute the snippet, sending it the properties you use in the tag.

Here's a nice custom output modifier developed by MODX Forum user michelle84. It removes or replaces unwanted words or phrases and can be used with any tag in MODX revolution. I've added some optional features that you can use to make it more flexible and easier to use.

The Snippet

Here is michelle84's original code:

$string = $input;
$output = str_replace(array('these', 'words', 'will', 'be', 'stripped'), '' , $string);
return $output;

To use it, you simply create a snippet called StripWords, paste the code above into the snippet, and use a tag like this:

[[*content:StripWords]]

The resource's content will be sent to the snippet as the $input variable. The snippet returns the $output variable, which MODX will use to replace the original value of the tag (in other words the value of the resource's content field).

Some Alternatives

You may object to having to edit the snippet code to change the list of stripped words, especially if it's a non-tech-savvy client who is doing the editing. One option would be to send the words to strip as a property in the snippet tag:

[[*content:Stripwords=`these,words,will,be,stripped`]]
$words = array_map('trim', explode(',', $options));
return str_replace($words, '' , $input);
The `array_map()` line above, demonstrates a useful function in PHP. The `array_map()` function takes an array as its second argument. It runs the function named in the first argument on each member of the array. It returns the altered array. In the example above, `array_map()` calls the `trim()` function on each member of the array created by `explode()`. The `trim()` function removes any extraneous leading or trailing spaces as well as a few other things like tabs and linefeeds.

The trouble with this option is that when you want to change the word list, you need to edit every instance of the StripWords output modifier on the site. No one wants that. One alternative would be to create a default property of the snippet called 'wordsToStrip' and put the comma-separated list of words as its value. In that case, the StripWords snippet would be called with just :StripWords, and its code would look like this:

$words = $modx->getOption('wordsToStrip', $scriptProperties, '');
if (!empty($words)) {
    $words = array_map('trim', explode(',', $words));
    return str_replace($words, '' , $input);
} else {
    return $input;
}

This might still challenge some users. A slightly slower, but more user-friendly way to go would be to put the word list in a chunk called WordsToStrip. All the user needs to do is edit the chunk to change the word list. If you are filtering out offensive words and phrases, this is handy, since site visitors will always be coming up with new ones.

Here's the WordsToStrip chunk:

these,words,will,be,stripped

Now, our snippet needs a small alteration to get the words from the chunk:

$words = $modx->getChunk('WordsToStrip');
$words = array_map('trim', explode(',', $words));
if (! empty($words)) {
    return str_replace($words, '' , $input);
} else {
    return $input;
}

Speed Considerations

If page-load speeds are a concern, it's much faster to alter the text when it's being saved to the database than to make the modifications on each page load, though it's not always practical. If the text you want to modify is the content field of a resource or is created in NewsPublisher, you can attach a plugin called StripWords to the OnDocFormSave System Event with code like this. This version, like the one above, uses the chunk to get the words.

$words = $modx->getChunk('wordsToStrip');
$words = array_map('trim', explode(',', $words));
$content = $resource->getContent();
$content = str_replace($words, '' , $content);
$resource->setContent($content);
$resource->save();
return '';

Unfortunately, this technique doesn't work with either Articles or Quip (at least not in the current versions). Quip does allow hooks, however, so it's possible that you could do it in a Quip hook.

Replacing the Strings

In all the examples above, the unwanted strings are replaced with an empty string. If you'd rather replace them with something else, you can change the empty string to whatever you want, like this:

return str_replace($words, '[expletive deleted]' , $input);

Coming Up

What if you want to replace each string with a different value? That requires creating two separate arrays for str_replace() to use. We'll look at how to do that in the next article.


Bob Ray is the author of the MODX: The Official Guide and dozens of MODX Extras including QuickEmail, NewsPublisher, SiteCheck, GoRevo, Personalize, EZfaq, MyComponent and many more. His website is Bob’s Guides. It not only includes a plethora of MODX tutorials but there are some really great bread recipes there, as well.