[wp-trac] [WordPress Trac] #63751: wpdb->set_prefix() throws deprecation notice for preg_match() when setup-config.php is loaded
WordPress Trac
noreply at wordpress.org
Fri Jul 25 19:12:58 UTC 2025
#63751: wpdb->set_prefix() throws deprecation notice for preg_match() when setup-
config.php is loaded
-------------------------------------------------+-------------------------
Reporter: jerclarke | Owner: (none)
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting
| Review
Component: Database | Version: 6.8.2
Severity: minor | Keywords:
Focuses: coding-standards, php-compatibility |
-------------------------------------------------+-------------------------
A bit esoteric, but this is throwing a reliable deprecation notice in my
logs when bots visit `/wp-admin/setup-config.php`:
{{{
[25-Jul-2025 18:59:23] PHP deprecated: "preg_match(): Passing null to
parameter #2 ($subject) of type string is deprecated" file: /[...]/wp-
includes/class-wpdb.php:1007 url: [...]/wp-admin/setup-config.php
}}}
The code throwing the error:
{{{
public function set_prefix( $prefix, $set_table_names = true ) {
if ( preg_match( '|[^a-z0-9_]|i', $prefix ) ) {
return new WP_Error( 'invalid_db_prefix', 'Invalid
database prefix' );
}
}}}
The immediate cause is obvious: `preg_match()` is run without checking
that `$prefix` has any content, and in PHP 8.x you're not allowed to pass
empty strings to it.
The "real" cause, I don't know. Why is there no `$prefix` available when
normally there would be? Should there be?
These `WP_INSTALLING` files like `setup-config.php` are a mystery to me,
and seem relatively untested especially with these PHP 8.x edge-cases.
My goal with this ticket is just to clean up the deprecation warning.
It seems to me all that needs to be done is to check that `!empty(
$prefix)` before doing the preg_replace(), so:
{{{
public function set_prefix( $prefix, $set_table_names = true ) {
if ($prefix && preg_match( '|[^a-z0-9_]|i', $prefix ) ) {
return new WP_Error( 'invalid_db_prefix', 'Invalid
database prefix' );
}
}}}
Seems to me that would resolve the notice and have the exact same effect
as the old code.
Thank you for helping fix this 🙏🏻
--
Ticket URL: <https://core.trac.wordpress.org/ticket/63751>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list