I just threw together a quick plugin to replace all "orphan" (invalid) & characters to & (ampersands). It will leave alone any HTML entity like or – or etc, but it will convert & to &
This is useful if your users enter a pagetitle like "News & Press", which Wayfinder will output exactly as that, without HTML encoding it first. This leaves your code invalid, and is kind of a pain to fix manually.
If anybody finds this useful or has any other ideas to enhance it please let me know. There might be something that does this already, but I couldn’t find it. Let me know if so. (It also contains a line to change ].]> to ]]>, which is useful if you’re using PHx and CDATA tags, mentioned in
another thread.)
//<?php
// Begin plugin code
$e = &$modx->event;
$id = $modx->event->params['id'];
if ($e->name == 'OnWebPagePrerender') {
$o = &$modx->documentOutput; // get a reference of the output
// Replace all ^text inside hats^ to <sup> tags, useful for more friendly Wayfinder replacements
$o = preg_replace('%\^([()a-zA-Z0-9 ]*)\^%i', "<sup>$1</sup>",$o);
// Replace orphan & with &
$o = preg_replace('%&(?![a-zA-Z0-9#]{1,6})%i', "&",$o);
// Replace ].]> with ]]>, useful for CDATA tags inside PHx calls: http://modxcms.com/forums/index.php?topic=31435.0
$o = str_replace('/* ].]> */', '/* ]]> */',$o);
}
$e->output($o);
return '';