Technical Blog

Daily stories at Systems MW – Wadih Maalouf

I was getting a WPML error today because it is using str_contains() without checking that the PHP version supports it:

[Thu Dec 14 14:33:03.200348 2023] [php7:error] [pid 3827109] [client 70.81.171.63:34334] PHP Fatal error: Uncaught Error: Call to undefined function str_contains() in /var/www/example.com/wp-content/plugins/sitepress-multilingual-cms/classes/request-handling/wpml-request.class.php:123\nStack trace:\n#0 /var/www/example.com/wp-content/plugins/sitepress-multilingual-cms/classes/request-handling/class-wpml-language-resolution.php(87): WPML_Request->show_hidden()\n#1 /var/www/example.com/wp-content/plugins/sitepress-multilingual-cms/classes/language/Detection/CookieLanguage.php(43): WPML_Language_Resolution->is_language_active()\n#2 /var/www/example.com/wp-content/plugins/sitepress-multilingual-cms/classes/request-handling/wpml-request.class.php(95): WPML\\Language\\Detection\\CookieLanguage->get()\n#3 /var/www/example.com/wp-content/plugins/sitepress-multilingual-cms/classes/language/Detection/Backend.php(45): WPML_Request->get_cookie_lang()\n#4 /var/www/example.com/wp-content/plugins/sitepress-multilingual-cms/vendor/wpml/collect/src/Illuminate/Support/helpers.php(26): WPML\\Language\\Detection\\Backend->WPML\\Langu in /var/www/example.com/wp-content/plugins/sitepress-multilingual-cms/classes/request-handling/wpml-request.class.php on line 123, referer: https://example.com/en/

To fix it urgently, I had to put a str_contains() polyfill right into wp-config.php:

if (!function_exists(‘str_contains’)) {
/**
* Check if substring is contained in string
*
* @param $haystack
* @param $needle
*
* @return bool
*/
function str_contains($haystack, $needle)
{
return (strpos($haystack, $needle) !== false);
}
}

I used the polyfill from gdarko at https://gist.github.com/gdarko/5dbcf96dccb2a3eece6c10d084d008af

And the site works now with that patch. 

The site is on PHP 7.4.33 and that version doesn’t have str_contains() yet.