New features, or no rush yet? Either way, it’s worth a try 🙂
PHP 8.6 came out on November 19. Not every environment supports it yet, but today we leave legacy code and old hosting versions out of it. We only look at what 8.6 really gives us in WordPress.
What can we do?
- Write shorter hooks: partial function application,
?instead of arrow functions. But a callback like that can no longer be removed withremove_filter(). - Sanitize settings in one call:
clamp()instead ofmax( min() ). - Write plugin config classes without a constructor:
readonlyproperties with defaults. - Find errors in `block.json`, `theme.json` and REST responses faster: JSON errors now show exactly where it broke.
What to check, even if you changed nothing:
- Sessions: the new, stricter defaults break SSO and returns from payment gateways in plugins that use PHP sessions.
- Deprecations: some functions now fill up
debug.log, and in PHP 9 they will become errors.
I know, you know, but we won’t tell anyone: we don’t really write code ourselves anymore. AI assistants do. But if we don’t tell the agent how to write it, because we don’t know what’s new ourselves, it will dump everything into
functions.phpthe same old way 🙂 — Anatoliy Dovgun, with love from a WordPress developer · 4WP.dev, for WordPress developers
1. Hooks with partial function application
// Before
add_filter( 'the_title', fn( $title ) => str_replace( 'WP', 'WordPress', $title ) );
// PHP 8.6
add_filter( 'the_title', str_replace( 'WP', 'WordPress', ? ) );
? means “an argument that comes later”. WordPress passes $title into it.
But: the result is a Closure, and it cannot be removed:
remove_filter( 'the_title', str_replace( 'WP', 'WordPress', ? ) ); // false
Every call creates a new object. A theme or another plugin will not be able to unhook your filter.
Use it for: internal logic, array_map(), array_filter(). Don’t use it for: a plugin’s public hooks. Those need a named function or a class method.
Why closures can’t be removed: lesson 3.2 remove_action() and Why Closures Stay.
2. clamp() for sanitizing settings
// Before
$per_page = max( 1, min( absint( $input['per_page'] ), 100 ) );
// PHP 8.6
$per_page = clamp( absint( $input['per_page'] ), min: 1, max: 100 );
Typical places: the sanitize_callback in register_setting(), the number of posts in a block, limits on REST parameters.
But: if you swap min and max, you get a ValueError, not a silently wrong number. That’s a good thing, but catch it in tests, not in production.
3. Plugin config: readonly with defaults
// Before
final class Settings {
public readonly int $cache_ttl;
public function __construct() {
$this->cache_ttl = HOUR_IN_SECONDS;
}
}
// PHP 8.6
final class Settings {
public readonly int $cache_ttl = HOUR_IN_SECONDS;
public readonly string $option_key = 'myplugin_settings';
}
Option keys, transient TTLs and slugs live in one place and can’t be changed by accident.
But: a default must be a constant expression. You can’t use get_option() or __() as a default; those still need a constructor.
Readonly, enums and
Requires PHP: lesson 2.3.
4. JSON shows where the error is
json_decode( file_get_contents( __DIR__ . '/block.json' ) );
echo json_last_error_msg(); // now includes the error position
An extra comma in block.json or theme.json, broken JSON in a response from an external API: before, you only got “Syntax error”; now you can see where it is. The same goes for JsonException.
What to check: sessions
New defaults: session.use_strict_mode = 1, session.cookie_httponly = 1, session.cookie_samesite = Lax.
WordPress core doesn’t use PHP sessions. Plugins do: forms, carts, LMS, SSO.
What breaks: a cross-site POST (SAML SSO, a return from a payment gateway) arrives without the session cookie. What to do: set SameSite=None; Secure explicitly for that flow, or switch it to a token.
What to check: deprecations
grep -rnE "is_integer|is_long|is_double|doubleval|spl_object_hash" ./my-plugin
is_integer(),is_long(),is_double(),doubleval()→is_int(),is_float(),floatval()spl_object_hash()→spl_object_id(). WordPress core already switched tospl_object_id()for hook callback IDs in version 7.1.returninsidefinally, and returning a value from a constructor.
How to read
debug.logand catch deprecations: lesson 1.3 Debugging.
In short
- PFA: for internal code, not for public hooks.
clamp(): in sanitization.readonlywith defaults: for plugin config.- Sessions and deprecations: check them now.
- New syntax: only together with
Requires PHP: 8.6.
Want to understand how PHP behaves inside WordPress, not just the syntax? Start with the free first module of the PHP for WordPress Developers course.
