Development

Getting Your Plugin Ready for PHP 8.6: What Breaks and How to Fix It

· 9 min read
wpbob-php-plugin-compatibility

PHP 8.6 came out on November 19. WordPress 7.2, due December 8, is fully compatible with it. Core contributors fixed every known issue before Beta 5, and every change to core now runs the full test suite on 8.6. Plugins are a different story. In the November PHP and WordPress contributor chat, the plugin ecosystem was called “the big unknown,” and the numbers back that up: one deprecated function alone appears nearly 9,000 times across more than 2,300 plugins in the directory. Here’s everything in PHP 8.6 that can break a WordPress plugin, how to find it in your code, and how to fix it so the same code still runs on PHP 7.4.

Why this can’t wait

WordPress used to label each new PHP version as having “beta support” for a while. That label has been retired. WordPress 7.2 supports PHP 8.6 on day one, and hosts that offer the newest PHP will start moving sites onto it. The minimum stays at PHP 7.4, so your plugin has to work across eight PHP versions at once.

Most of what follows doesn’t crash anything. It shows up as deprecation notices in logs, black thumbnails, rejected font uploads, or file saves that fail where they used to succeed. Those are the bugs that reach users before they reach you.

The checklist

Run each search from your plugin’s root folder. Each section shows what to look for, why it breaks, and the fix.

1. spl_object_hash() is deprecated

grep -rn "spl_object_hash" --include="*.php" .

PHP 8.6 deprecates spl_object_hash(). It still works, but every call raises a deprecation notice, and deprecated functions are usually removed in the next major version. The replacement is spl_object_id(), which has existed since PHP 7.2, so it works on WordPress’s 7.4 minimum.

The catch: spl_object_hash() returns a 32-character string, and spl_object_id() returns an integer. A straight swap works when the value is only used as an array key or concatenated into a string:

// Before
$key = spl_object_hash( $callback[0] ) . $callback[1];

// After
$key = spl_object_id( $callback[0] ) . '::' . $callback[1];

The '::' separator matters. With an integer ID and no separator, object 1 with method 2run and object 12 with method run produce the same key.

Look harder anywhere the code checks the length or format of the value, type-hints it as a string, or compares it with === to a stored string. Also look for code that saves the value in an option, transient or cache. Both functions return values that are only unique while the object exists, and IDs are reused once it’s destroyed. Storing either one beyond the current request was already a bug. The switch is a good time to fix it.

2. TrueType fonts have a new MIME type

grep -rn "font/sfnt" .

PHP 8.6 updates its bundled copy of libmagic, the library behind file type detection. A valid .ttf file used to be reported as font/sfnt. On 8.6 it’s font/ttf. Some older PHP builds that use the system’s libmagic already return font/ttf, so you can see either one on any version.

Core now accepts both. Your plugin’s own checks should too:

// Before
if ( 'font/sfnt' === $mime_type ) { ... }

// After
if ( in_array( $mime_type, array( 'font/ttf', 'font/sfnt' ), true ) ) { ... }

Check upload_mimes filters as well. If your plugin maps ttf to font/sfnt, valid fonts uploaded on 8.6 won’t match. This hits font managers, page builders and anything that lets users upload custom fonts.

3. Flipping images with negative dimensions now makes black images

grep -rn "imagecopyresampled\|imagecopyresized" --include="*.php" .

For years, a common GD trick was to flip an image by copying it with negative width or height. Core’s own GD editor did this. PHP 8.6 bundles GD 2.4.0, and with negative values imagecopyresampled() now returns an all-black image. Core hit this too: every GD flip produced a black image, including the automatic flip of JPEGs with certain EXIF orientations. 7.2 fixes it in core.

If your plugin copied the trick, look for a negative source width or height in the results above, and switch to imageflip(), which has been in PHP since 5.5:

// Before: horizontal flip by copying with a negative width
imagecopyresampled( $dst, $src, 0, 0, $w - 1, 0, $w, $h, -$w, $h );

// After
imageflip( $src, IMG_FLIP_HORIZONTAL ); // or IMG_FLIP_VERTICAL, IMG_FLIP_BOTH

imageflip() changes the image in place and returns true or false, so you no longer need the second image resource. If you only process images through wp_get_image_editor(), you’re covered by core’s fix.

4. Stream wrappers need stream_flush()

grep -rn "stream_wrapper_register" --include="*.php" .

In PHP 8.6, fclose(), file_put_contents() and copy() return false when flushing or closing a stream fails. PHP treats a custom stream wrapper with no stream_flush() method as a failed flush, even when every byte was written. So any write through your wrapper now reports failure.

This mostly affects offloading and storage plugins, the ones that register an s3://-style wrapper so WordPress can save uploads to remote storage. On 8.6, image saves through such a wrapper fail with an image_save_error. The fix is one method on the wrapper class:

public function stream_flush() {
    return true; // Nothing buffered.
}

If your wrapper buffers data and sends it on flush, return the real result of sending it. This change works in your favor: in 7.2, core also checks these return values for GD saves. Before, a failed upload to remote storage could be reported as a success. Now it’s an error you can see and handle.

5. clamp() throws different exceptions on different PHP versions

grep -rn "clamp(" --include="*.php" .

PHP 8.6 adds a native clamp() function to keep a value within a range. WordPress has had a polyfill since 7.1, so you can use it on every supported PHP version. On invalid arguments, native PHP 8.x throws a ValueError. On PHP 7.4, where ValueError doesn’t exist, the polyfill throws an InvalidArgumentException. Catch both:

try {
    $value = clamp( $value, $min, $max );
} catch ( ValueError | InvalidArgumentException $e ) {
    $value = $min;
}

On PHP 7.4, naming a class that doesn’t exist in a catch is fine. PHP only compares the exception against it.

6. Your test suite

The last spl_object_hash() call in WordPress was in the PHPUnit test library, in the helper behind tests_add_filter(). Most plugin test bootstraps use that helper, including the one wp scaffold plugin-tests generates. It’s fixed in the 7.2 test library, and the fix won’t be backported. Run PHP 8.6 only against WordPress 7.2 or later, or your tests will fill up with deprecation notices that aren’t your code’s fault.

Adding PHP 8.6 to CI

Test the range you claim to support, not just the newest version: the lowest PHP version in your Requires PHP header, one in the middle, and 8.6. A GitHub Actions matrix for a plugin scaffolded with WP-CLI:

jobs:
  test:
    runs-on: ubuntu-latest
    services:
      mysql:
        image: mysql:8.0
        env:
          MYSQL_ROOT_PASSWORD: root
        ports: [ '3306:3306' ]
    strategy:
      fail-fast: false
      matrix:
        include:
          - { php: '7.4', wp: 'latest' }
          - { php: '8.3', wp: 'latest' }
          - { php: '8.6', wp: 'trunk' }  # use 'latest' once 7.2 is out
    steps:
      - uses: actions/checkout@v4
      - uses: shivammathur/setup-php@v2
        with:
          php-version: ${{ matrix.php }}
      - run: composer install
      - run: bash bin/install-wp-tests.sh wordpress_test root root 127.0.0.1 ${{ matrix.wp }}
      - run: vendor/bin/phpunit

Until December 8, latest is 7.1, so the 8.6 job tests against trunk. Switch it to latest once 7.2 is out. Then make deprecations fail the build, not just print. A deprecation that scrolls past in CI is a support ticket later. One way is to convert them to exceptions in your test bootstrap:

set_error_handler( static function ( $errno, $errstr, $file, $line ) {
    if ( E_DEPRECATED === $errno || E_USER_DEPRECATED === $errno ) {
        throw new ErrorException( $errstr, 0, $errno, $file, $line );
    }
    return false;
} );

Add it after WordPress has loaded, since PHPUnit and WordPress both set their own handlers, and scope it to your own code if a dependency is noisy. If you use wordpress-develop’s Docker environment, an 8.6 image is available: run npm run env:pull, then LOCAL_PHP=8.6-fpm npm run env:start.

Should you raise your minimum PHP version?

WordPress keeps PHP 7.4 as its minimum, but your plugin doesn’t have to. The Requires PHP header in your main plugin file controls who can install and update it. WordPress won’t offer an update to a site running an older version.

From the November contributor chat: about 14.5% of WordPress sites are still on PHP 7.4, about 3% on 8.0 and about 9% on 8.1. PHP 8.2 is approaching the end of its security support. Supporting all of them means every line of your code has to run on a version that hasn’t had a security fix in years. Contributors encouraged plugin authors to raise their own minimums, and to give hosts and site owners a reason to upgrade.

There’s one trade-off to plan for. When you raise Requires PHP, sites below it stop getting your updates, including security fixes. If you raise it, announce it a release ahead, and be ready to ship a security fix for the older branch if something serious turns up. Don’t strand users on a version you know is vulnerable. That aside, a plugin that requires 8.1 or later can drop polyfills, use typed properties and enums, and test fewer combinations. You don’t have to wait for core.

Before you tag a release

  1. Run the five searches above and fix what they find.
  2. Run your test suite on PHP 8.6 against WordPress 7.2 (RC or trunk until December 8).
  3. Activate the plugin on a PHP 8.6 site with WP_DEBUG and WP_DEBUG_LOG on, click through every screen, and read wp-content/debug.log.
  4. Upload a .ttf font and a rotated phone photo if your plugin touches fonts or images.
  5. Bump “Tested up to” to 7.2 only after all of that passes.

Skim PHP’s UPGRADING notes and the deprecations RFC too. The list above covers what affects WordPress; your plugin may use something else that changed.

The bigger lesson

Almost every item here has the same cause: code that relied on behavior PHP never promised. Negative dimensions to flip an image, a stream wrapper without a flush method, a MIME string hard-coded from one version of a library. They all worked until they didn’t. Core relied on the image flip trick itself. The fix each time was to use the function built for the job. Running the newest PHP in CI and the oldest one you support is how you find those habits before users do, and part of how a plugin earns its place. For why your PHP version matters beyond compatibility, see Your Host’s PHP Version Is a Security Setting.

Sources