0

I am using PHP 7.4.33 and WPML auto updated itself and appears to be using str_contains() although the PHP version doesn't support it yet.

The error in the apache logs are:

[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/

Is there a way I can urgently repair it or patch it?

This is a self-responded answer in case someone comes across the same issue, please find answer below.

5
  • If you're using WordPress 5.9 and up, there should be a polyfill in place for you, in the file wp-includes/compat.php.
    – Pat J
    Commented Dec 14, 2023 at 20:52
  • @PatJ Yes. However the installation in that case did not have WordPress 5.9, because the theme was not compatible, so it was frozen for a few more months before being rebuilt. In case someone else is in that difficult situation, which I found a few threads online asking the same question, which is why I posted the answer for those edge cases.
    – Wadih M.
    Commented Dec 14, 2023 at 21:06
  • str_contains was introduced in PHP 8, you can't use it on PHP 7.x, you have to upgrade to v8 or downgrade WPML. Note that questions about 3rd party plugins are offtopic though, you can't ask for help with WPML here
    – Tom J Nowell
    Commented Dec 14, 2023 at 21:16
  • @TomJNowell noted, thanks for your addition. I saw there was only 17 posts site-wide on wpml so I figured not much have been posted. But I'm sure this specific case will be helpful based on the traffic this question was getting online, I'm positive a lot of websites are currently broken. Furthermore it gives an a quick educational example of a polyfill. But I will make sure to do this sparingly.
    – Wadih M.
    Commented Dec 14, 2023 at 21:43
  • 1
    Note that even if the question is the most popular question that has ever been asked on the site, if it's offtopic it will be closed
    – Tom J Nowell
    Commented Dec 15, 2023 at 10:26

1 Answer 1

2

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.

Note that if you are having that error, you probably have a WordPress version under 5.9 as Pat J pointed out in the comments, and PHP before version 8. You might be stuck with that version for the time being, so the solution above can be a temporary urgent patch to resolve.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.