Hi, I am trying to replace all instances of a specific string with a random image from a ‘pictograms’ page.
I am using a kirbytext pre filter for this:
<?php
$img = $pages->find('pictograms')->images()->shuffe()->first()->url();
kirbytext::$pre[] = function($kirbytext, $value) {
return str_replace('to be replaced', '<img src="'.$image.'"</span>', $value);
};
?>
In the debugger I get the error ‘undefined variable: pages’. I came across this thread and tried to use kirby()->$pages instead but this isn’t working either.
Can someone point me in the right direction?
Edit: solved thanks to the answers. Working code below minus embarassing typos:
<?php
kirbytext::$pre[] = function($kirbytext, $value) {
$img = kirby()->site()->pages->find('pictograms')->images()->shuffe()->first();
return str_replace('to be replaced',$img, $value);
};
?>