#63627 closed enhancement (fixed)
General: Only override `$table_prefix` if not defined
| Reported by: |
|
Owned by: |
|
|---|---|---|---|
| Milestone: | 7.0 | Priority: | normal |
| Severity: | normal | Version: | 6.9 |
| Component: | Bootstrap/Load | Keywords: | has-patch |
| Focuses: | php-compatibility | Cc: |
Description
Only set the variable $table_prefix, if set before (e.g. via some kind of custom bootstrapper).
To use ??= operator we need >= php8.4, but happily referring to your documentation, this is already the current minimum requirement :D
https://wordpress.org/about/requirements/
-> bumped version in composer.json
Ref: https://github.com/WordPress/wordpress-develop/pull/9087
Change History (11)
This ticket was mentioned in PR #9087 on WordPress/wordpress-develop by @Ninos Ego.
10 months ago
#1
- Keywords has-patch added
#2
@
10 months ago
Hi @ninos-ego,
Could you expand on the reasoning for the change? What are you attempting to achieve, what problem does this solve? etc
#3
@
6 months ago
Sorry for the late response...
I'm using some kind of wordpress bootstrapper to preload some environment configs before initializing wordpress, e.g. database, url, proxy, settings, ... Useful at least for containerized environments.
My current workaround is writing following line of code before including wp-settings.php:
$table_prefix = $GLOBALS['table_prefix'];
Its not much line of codes, but I want to get rid of it (object-oriented coding). My patch will not break anything, it just gives a bit more flexibility initializing wordpress :-)
#4
@
5 months ago
- Component changed from General to Bootstrap/Load
- Milestone changed from Awaiting Review to 7.0
#5
@
5 months ago
- Owner set to SergeyBiryukov
- Resolution set to fixed
- Status changed from new to closed
In 61250:
#6
@
5 weeks ago
This just came up in when attempting to raise the PHPStan rule level to 1 (#64238).
In the committed code:
<?php if ( ! isset( $GLOBALS['table_prefix'] ) ) { $GLOBALS['table_prefix'] = $table_prefix; }
There is a PHPStan error:
Variable $table_prefix might not be defined.
But I'm struggling to understand what the logic is supposed to be doing. At a glance it looks like dead code. In other words, $GLOBALS['table_prefix'] would seem to be an alias for $table_prefix. Otherwise, how is $table_prefix populated?
The proposed fix is to add global $table_prefix, but I'm wary this may undo what was intending to be done here.
Please see full context: https://github.com/WordPress/wordpress-develop/pull/11189/changes#r2898980934
#7
@
5 weeks ago
@westonruter I'm using some kind of bootstrapper, see the code part for that:
https://ego.codes/3p5/library/bootstrap/-/blame/develop/src/Service/Configuration.php?ref_type=heads#L29
#8
@
5 weeks ago
@ninos-ego thank you. I see in your bootstrap code that you explicitly are already doing global $table_prefix;
So is the if statement actually executing for you?
if ( ! isset( $GLOBALS['table_prefix'] ) ) {
$GLOBALS['table_prefix'] = $table_prefix;
}
To me it seems it will already be a global so it will already be set.
#9
@
5 weeks ago
@westonruter I'm running the bootstrapping part before any wordpress code, so without the if-statement it overwrites the global variable... :/
#10
@
5 weeks ago
@ninos-ego ok, then it's safe to say that adding global $table_prefix as proposed in the fix to modify wp-settings.php would break your bootstrapping logic?
- global $wp_version, $wp_db_version, $tinymce_version, $required_php_version, $required_php_extensions, $required_mysql_version, $wp_local_package; + global $wp_version, $wp_db_version, $tinymce_version, $required_php_version, $required_php_extensions, $required_mysql_version, $wp_local_package, $wp_filter, $table_prefix;
See my comment.
#11
@
5 weeks ago
@ninos-ego Since the changes touch code you have recently contributed, please test these change to ensure they do not cause any regressions for you: https://github.com/WordPress/wordpress-develop/pull/11189
Only set the variable
$table_prefix, if set before (e.g. via some kind of custom bootstrapper).To use
??=operator we need>= php8.4, but happily referring to your documentation, this is already the current minimum requirement :Dhttps://wordpress.org/about/requirements/
-> bumped version in
composer.jsonTrac:
https://core.trac.wordpress.org/ticket/63627