Seeing a fatal error like this when you submit a Contact Form 7 form?
PHP Fatal error: Uncaught Error: Call to undefined function str_starts_with()
in /wp-content/plugins/contact-form-7/includes/rest-api.php:331
In plain English, Contact Form 7 is calling str_starts_with(), which is a PHP 8 only function, but your form submission is actually running on PHP 7.x.
So the real problem is not the form. It is a PHP version mismatch between what Contact Form 7 expects and what your server is using for that request.
Below is a safe path to check your real PHP version, upgrade it for the site that serves Contact Form 7, and short term workarounds if you cannot upgrade yet.
Contact Form 7 is throwing “PHP Fatal error: Uncaught Error: Call to undefined function str_starts_with() in /wp-content/plugins/contact-form-7/includes/rest-api.php:331” when I submit the form. I am on PHP 7.4. How do I fix this?
Short answer
Contact Form 7 now uses str_starts_with() in its REST API code.
str_starts_with() only exists in PHP 8 and higher.
If you see “Call to undefined function str_starts_with()” in includes/rest-api.php then your form submission is actually running on PHP 7.x. Very often PHP 7.4.
The clean fix is to:
Use this map if you want to jump around.
str_starts_with() polyfill on PHP 7.xYour stack trace will look similar to the one from the Contact Form 7 support forums.
PHP Fatal error: Uncaught Error: Call to undefined function str_starts_with()
in /wp-content/plugins/contact-form-7/includes/rest-api.php:331
Stack trace:
#0 /wp-includes/rest-api/class-wp-rest-server.php(1140): WPCF7_REST_Controller->create_feedback()
#1 /wp-includes/rest-api/class-wp-rest-server.php(987): WP_REST_Server->respond_to_request()
...
WordPress calls the Contact Form 7 REST endpoint when you submit a form.
Inside that REST controller Contact Form 7 calls str_starts_with(). This function is part of the PHP 8 string helpers.
On PHP 7.x that function does not exist. So PHP stops everything with a fatal error.
That is why the form spinner keeps spinning and your error log shows this fatal. Once the fatal is gone Contact Form 7 can send its JSON response back to the browser again.
Hosts sometimes show one PHP version in their panel but run another one for the actual site. It is common to see PHP 7.4 still active for the web server even when the host says you are on 8.0.
If this says 8.0, 8.1, or 8.2 then WordPress itself believes it is on PHP 8.
If it says 7.4 or anything lower, that already explains the fatal. Contact Form 7 is calling a function that does not exist in that PHP version.
In that case the request that handles the form may still be going through a different PHP handler.
Useful checks.
.htaccess for lines that force an older PHP version for this site. For example a handler string that mentions php74./wp-json/ REST routes.Once you know the real version that runs your forms, you can plan the upgrade.
Contact Form 7 follows the PHP versions recommended by recent WordPress releases. The current plugin releases are developed and tested with PHP 7.4 and newer, with strong support for PHP 8 family.
For this specific error you need at least PHP 8.0 because that is when str_starts_with() was added to PHP itself.
Avoid staying on PHP 7.4 long term. It is end of life, so it no longer receives security fixes.
The exact steps depend on your host, but it usually looks like this.
If your host lets you choose per directory, be sure the document root that contains wp-config.php and wp-content is on PHP 8.
Then test your Contact Form 7 form again.
str_starts_with() polyfill if you are stuck on PHP 7.xSometimes you cannot upgrade PHP right away. For example if another critical plugin does not yet support PHP 8.
In that case you can add a small compatibility function so Contact Form 7 can call str_starts_with() even on PHP 7.x.
This is a workaround. Use it only as a temporary measure while you plan a real PHP upgrade.
wp-content create a folder mu-plugins if it does not exist.mu-plugins create a file named cf7-str-starts-with-polyfill.php.<?php
/**
* Contact Form 7 compatibility for PHP 7.x
* Adds str_starts_with() if it does not exist yet.
*/
if ( ! function_exists( 'str_starts_with' ) ) {
function str_starts_with( string $haystack, string $needle ): bool {
return $needle === '' || strpos( $haystack, $needle ) === 0;
}
}
Files in mu-plugins load automatically on every request. This adds str_starts_with() before Contact Form 7 runs.
After you add the file, submit your form again.
str_starts_with().Again, this keeps your forms alive but does not change the fact that PHP 7.4 is end of life. Plan to upgrade.
This is the least attractive option, but sometimes it is all you can do in the short term.
Older Contact Form 7 versions did not call str_starts_with(). So they can run on PHP 7.4 without this specific fatal.
The tradeoff is that older plugin versions miss security fixes and new features. You should only do this when you cannot update PHP yet and you understand the risk.
str_starts_with().Once you can move your site to PHP 8, update Contact Form 7 to the latest version again and remove any polyfill or rollback.
Contact Form 7 uses the WordPress REST API to submit forms by default.
When str_starts_with() fails, you see both the fatal in logs and a JavaScript error in the browser console.
/wp-json/contact-form-7/v1/contact-forms/<id>/feedbackBefore the fix, that request fails and the response is a PHP fatal page or a generic 500 HTML.
After the fix, you should see a JSON response with a structure similar to this.
{
"status": "mail_sent",
"message": "Thank you for your message. It has been sent."
}
Once you see that, Contact Form 7 can show its usual success or error message on the page.
You are in good shape when all of these are true.
Call to undefined function str_starts_with() from contact-form-7/includes/rest-api.php.If you are still getting the fatal or a blank form response, I can help you read your logs and host settings.
Hit Continue Chat below and send:
With that I can help you choose the safest fix for your setup.
Scroll down to the contact form below. Enter your name, email, and WordPress needs. Atiba will get back to you as soon as possible.
WP Assistant is a free tool created by Atiba Software, a WordPress design and development company located in Nashville, TN. If you need more personalized WordPress assistance let us know, and we’ll get back to you ASAP!