Scratch that - that code actually replaces the first instance of the keyword in every snippet/chunk/document on your page.
So my menu is a chuck so it replaces it in my menu.
My content is a document so the first instance was replaced.
I have a chuck for a contact box on the left hand side and code replaced the first instance of the keyword in that as well.
The only workaround I can think of is on your document which is what you want this code to run on add a comment such as <!-- Begin Content -->
Search for this and if found do the code else don’t bother
<?php
$output = $modx->documentOutput;
if(strpos($output, '<!-- Begin Content -->') !== false)
{
if (!function_exists('str_replace_once')) {
function str_replace_once($needle , $replace , $haystack){
// Looks for the first occurence of $needle in $haystack
// and replaces it with $replace.
$pos = strpos($haystack, $needle);
if ($pos === false) {
// Nothing found
return $haystack;
}
return substr_replace($haystack, $replace, $pos, strlen($needle));
}
}
$replacementText = ' <a href="machinery-safety">machinery safety</a>';
if (strpos($output, $replacementText ) === false)
$modx->documentOutput = str_replace_once(' machinery safety', $replacementText, $output);
}
?>
For the above to work in the document / chunk you want this to work in add the following: