Custom PHP script throws critical error ONLY when editing page
I have the following script added to functions.php in my child theme:
function getlatestfile() {
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator('wp-content/uploads/cams/'));
foreach ($iterator as $file) {
if ($file-isDir()) continue;
$path = $file-getPathname();
}
return img src='https://mysite.url/ . $path . ' /;
}
// register shortcode
add_shortcode('getfieldimage', 'getlatestfile');
I can save the code, insert the shortcode [getfieldimage]
on the page, and the page indeed displays the latest image. No complains.
But when trying to edit again the page containing the shortcode, WP tells me that there's a critical error, and wants me to go to https://wordpress.org/support/article/faq-troubleshooting/ . Cannot get any helpful info there.