Fix “Call to undefined function str_starts_with()” in Contact Form 7

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:

  • Confirm which PHP version your site is really using
  • Update that site to PHP 8.0 or higher
  • Only use polyfills or plugin downgrades as short term workarounds

Use this map if you want to jump around.


What this error really means

Your 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.


Step 1. Check the PHP version WordPress is really using

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.

Check inside WordPress

  1. Log in to your WordPress admin.
  2. Go to Tools → Site Health → Info.
  3. Expand the Server section.
  4. Look for PHP version.

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.

If Site Health says PHP 8 but you still get the error

In that case the request that handles the form may still be going through a different PHP handler.

Useful checks.

  • On cPanel or Plesk hosting, look for a PHP version selector per domain or per directory.
  • Check your .htaccess for lines that force an older PHP version for this site. For example a handler string that mentions php74.
  • If your host gives you multiple PHP versions, ask them to confirm which one serves your public site and the /wp-json/ REST routes.

Once you know the real version that runs your forms, you can plan the upgrade.


Step 2. Upgrade PHP to a Contact Form 7 friendly version

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.

Pick a target version

  • If you want a safe baseline, choose PHP 8.0 or 8.1.
  • If your theme and plugins list compatibility with 8.2, that is even better.

Avoid staying on PHP 7.4 long term. It is end of life, so it no longer receives security fixes.

Update PHP in your hosting panel

The exact steps depend on your host, but it usually looks like this.

  1. Log in to your hosting control panel.
  2. Find the PHP selector section. Common names are:
    • Software → MultiPHP Manager
    • Software → Select PHP version
    • Websites → PHP Settings
  3. Select your domain that runs WordPress.
  4. Change the PHP version to 8.0 or higher.
  5. Apply or save the change.

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.

Check that WordPress sees the change

  1. Return to Tools → Site Health → Info → Server.
  2. Confirm that the PHP version field now shows your new PHP 8 version.

Then test your Contact Form 7 form again.

  • If the fatal error disappears and the form submits, you have fixed the core issue.
  • If you get a different error, capture it. You might have another plugin that is not ready for PHP 8 yet and needs an update of its own.

Step 3. Add a short term str_starts_with() polyfill if you are stuck on PHP 7.x

Sometimes 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.

Create a tiny compatibility plugin

  1. In wp-content create a folder mu-plugins if it does not exist.
  2. Inside mu-plugins create a file named cf7-str-starts-with-polyfill.php.
  3. Put this content inside it.
<?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.

  • If the fatal error disappears and the form submits, your polyfill is working.
  • Once you later upgrade to PHP 8, this function will be ignored because PHP already provides 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.


Step 4. Downgrade Contact Form 7 only if you truly cannot upgrade PHP

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.

Use WP Rollback or install an older version manually

  1. Install the WP Rollback plugin if you want a simple UI. It lets you choose an older Contact Form 7 release from the WordPress directory.
  2. Pick a version that predates the REST code path that uses str_starts_with().
  3. After downgrading, test your form.

Once you can move your site to PHP 8, update Contact Form 7 to the latest version again and remove any polyfill or rollback.


Step 5. Check that REST submissions work after the fix

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.

Check the network response

  1. Open your contact page in a private window.
  2. Open browser developer tools and switch to the Network tab.
  3. Submit the form.
  4. Look for a request to a URL like:/wp-json/contact-form-7/v1/contact-forms/<id>/feedback

Before 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.


Verification checklist

You are in good shape when all of these are true.

  • Your error log no longer shows Call to undefined function str_starts_with() from contact-form-7/includes/rest-api.php.
  • Tools → Site Health → Info → Server shows a PHP 8 version for your WordPress site, or you have a working short term polyfill while you plan that upgrade.
  • Contact Form 7 forms submit. The spinner stops. You see a success or validation error message instead of a silent failure.
  • The REST request to the Contact Form 7 endpoint returns JSON rather than a fatal error page.

Still stuck

For AI help

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:

  1. The exact fatal error line from your PHP or server log.
  2. The PHP version reported in Tools → Site Health → Info → Server.
  3. Whether you tried upgrading PHP, adding the polyfill, or downgrading Contact Form 7.
  4. Your hosting provider name and a screenshot or copy of any PHP version selector you use.

With that I can help you choose the safest fix for your setup.

For expert human help

Scroll down to the contact form below. Enter your name, email, and WordPress needs. Atiba will get back to you as soon as possible.

Need human WordPress help?

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!