Getting Your WordPress Plugins and Themes Ready for PHP 8

On Monday, WordPress core contributor Jonathan Desrosiers published a detailed post on the Make WordPress Core blog about the upcoming PHP 8 release and how it affects WordPress.

PHP 8 Is Coming

Scheduled for release on November 26, 2020, PHP 8 is the next major update to our favorite scripting language. While previous PHP releases have not had too much of an adverse effect on the WordPress ecosystem, this update has some breaking changes that could affect backward compatibility. It should also be noted that many features that were deprecated in PHP 7.x will now be removed in PHP 8.

The Status of WordPress Core

In his post, Desrosiers highlights the work that has been done to keep the core software up to date. “WordPress Core aims to be compatible with PHP 8.0 in the 5.6 release (currently scheduled for December 8, 2020),” he wrote.

However, this does not mean it is safe to upgrade to PHP 8 when WordPress 5.6 is released. WordPress is rarely run just on its own and usually relies on at least one theme and a collection of plugins to function as a blog or web site. As such, he points out, “The state of PHP 8 support within the broader ecosystem (plugins, themes, etc.) is impossible to know. For that reason, WordPress 5.6 should be considered ‘beta compatible’ with PHP 8.“

What this means, essentially, is that until most major themes and plugins are PHP 8 compatible, WordPress cannot be considered fully compatible.

Understand How PHP 8 Could Affect Your Plugin or Theme

Companies like Yoast have been preparing for this for a little while now. In late October, Yoast CTO Omar Reiss, along with fellow contributors Juliette Reinders Folmer, maintainer of the WordPress Coding Standards Sniffs for PHPCS, and Yoast DevOps manager Herre Groen, compiled and published a comprehensive WordPress/PHP 8 compatibility report.

While I highly recommend you take the time to read through the entire report, it does outline the main reason that the PHP 8 upgrade could have such a drastic effect on large WordPress sites, especially the plugin and theme ecosystem.

“However, PHP 7.* versions have seen a far larger set of deprecations than previous versions of PHP. Where PHP 5.6 to PHP 7 was a relatively simple migration, going from 7.x to 8 could be very painful, especially for very old codebases, like WordPress and many of the plugins that are available for it. For well-typed codebases or codebases which have stayed up-to-date with the latest PHP versions, there isn’t a big problem.”

As a maintainer of a few plugins, some built on code dating back eight years, it is worrisome that this upgrade could cause sites to break.

PHPCompatibility repository.

How to Prepare Yourself

I asked Reiss and Folmer what plugin and theme developers can do to get ready, and they shared some pointers.

First and foremost, developers should inform themselves about the changes coming in PHP 8: read the Make post about PHP 8, read the Yoast PHP8 Compatibility report, read the “Migrating from PHP 7.4 to PHP 8.0” section of the PHP manual, and potentially dig deeper by reading the UPGRADING doc in the PHP 8 branch and the RFCs for PHP 8.

Some available tools can be used to help look for incompatibilities:

Run PHP lint on PHP 8 over their code, either via the php -l command (making sure to iterate over all files) or by using PHP Parallel Lint.

Run PHPCompatibility over their code: it should be noted that nearly all PHP 8 related sniffs are in the as-of-yet-not-yet-released version 10.0.0 of PHPCompatibility, so people would need to use the develop branch or via Composer dev-develop for the time being, until version 10.0.0 is released.

Run the unit/integration tests for the plugin or theme on PHP 8 and fix anything which that comes up as an error. This will often mean that the test suite first needs to be made compatible with PHPUnit 9.3+. The PHPUnit Polyfills package and WP Test Utils package (both published under the Yoast GitHub organization) can help with this. It’s also important to note a considerable test coverage is needed to make this reliable.

Run the WordPress unit tests and WordPress e2e tests with your plugin activated, and fix any issues that arise.

Check whether the (strict) code coverage of said tests is high enough and if not, add more tests, making sure both happy and unhappy paths are covered.

If there are no tests, test everything manually, focusing especially on the “unhappy paths”, and expect to receive bug reports for the foreseeable future. At the same time, this is probably a good time to look into implementing unit/integration tests for your plugin or theme.

There Is Still Time, But It Is Running Out

As Desrosiers pointed out in the Make post, WordPress is only officially aiming to be PHP 8 ready by the time 5.6 is released in early December. Potentially, this means that many WordPress-focused hosting companies will only consider offering upgrades to their customers once WordPress core is compatible. So as plugin and theme developers, we have some time to test our products and get them ready, but that window is closing fast.

Fortunately for us, the knowledge and tools to get up to date are out there. We merely need to put them into action.