Fix incorrect home/siteurl when WP-CLI runs from root filesystem#307
Fix incorrect home/siteurl when WP-CLI runs from root filesystem#307
Conversation
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
There was a problem hiding this comment.
Pull request overview
Fixes malformed home/siteurl values during wp core install when WP-CLI is executed from the filesystem root by ensuring WordPress’s URL guessing logic sees server paths that reflect the --url value.
Changes:
- Override
$_SERVER['PHP_SELF']/$_SERVER['SCRIPT_NAME'](and conditionally$_SERVER['SCRIPT_FILENAME']) during installation based on the parsed--urlpath. - Normalize URL paths to an
index.php-style script path for WordPress’s install-time URL detection. - Add Behat coverage for installs with
--urlvalues with and without a path component.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
src/Core_Command.php |
Adjusts server superglobals during install so WP’s wp_guess_url() derives the correct base URL. |
features/core-install.feature |
Adds scenarios asserting home/siteurl values for URLs with and without a path. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
This comment was marked as resolved.
This comment was marked as resolved.
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
…e do_install() call Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
There was a problem hiding this comment.
Code Review
The pull request effectively addresses the issue of incorrect home and siteurl values when WP-CLI is executed from the root filesystem. The solution correctly manipulates $_SERVER variables to ensure WordPress's URL detection functions behave as expected. The addition of comprehensive Behat tests is a significant improvement, verifying the fix across various URL structures. The use of pathinfo() and Utils\parse_url() contributes to the robustness and security of the solution.
| $url_parts = Utils\parse_url( $assoc_args['url'] ); | ||
| $path = isset( $url_parts['path'] ) ? $url_parts['path'] : '/'; | ||
|
|
||
| // Ensure path represents a PHP script for proper WordPress URL detection. | ||
| $path = rtrim( $path, '/' ); | ||
| if ( empty( $path ) ) { | ||
| $path = '/index.php'; | ||
| } elseif ( '' === pathinfo( $path, PATHINFO_EXTENSION ) ) { | ||
| $path .= '/index.php'; | ||
| } | ||
|
|
||
| $_SERVER['PHP_SELF'] = $path; | ||
| $_SERVER['SCRIPT_NAME'] = $path; | ||
|
|
||
| // Set SCRIPT_FILENAME to the actual WordPress index.php if available. | ||
| if ( file_exists( Utils\trailingslashit( ABSPATH ) . 'index.php' ) ) { | ||
| $_SERVER['SCRIPT_FILENAME'] = Utils\trailingslashit( ABSPATH ) . 'index.php'; | ||
| } |
There was a problem hiding this comment.
The logic for setting $_SERVER variables based on the provided URL is duplicated across install(), multisite_install(), and do_install(). While the layered approach is understandable for defense in depth, extracting this common logic into a private helper method (e.g., setServerUrlVars($url)) would improve maintainability by reducing redundancy and ensuring consistency if this logic ever needs to be updated. This would make the code cleaner and easier to manage.
$this->setServerUrlVars( $assoc_args['url'] );
Fix "core install" URL when wp-cli is executed from root filesystem
Resolves the issue where
wp core installdefines incorrect home and siteurl values when WP-CLI is executed from the root of the filesystem.Problem
When WP-CLI phar is located at the root of the filesystem (e.g.,
/wp) and WordPress is installed at a different path (e.g.,/home/customer/website), thewp core installcommand sets incorrecthomeandsiteurlvalues. Instead of using the URL provided via the--urlparameter (e.g.,https://website.domain.tld), it produces malformed URLs likehttps://website.domain.tldhome/customer/website.Root Cause
WordPress's internal URL detection functions (like
wp_guess_url()) use$_SERVER['PHP_SELF']and$_SERVER['SCRIPT_NAME']to determine the script path. When WP-CLI is executed from/wp, these variables contain/wp(the path to the WP-CLI phar) rather than the WordPress installation path, which causes incorrect URL construction duringwp_install().Solution
The fix sets
$_SERVER['PHP_SELF'],$_SERVER['SCRIPT_NAME'], and$_SERVER['SCRIPT_FILENAME']to match the URL path provided via the--urlparameter. The fix is now applied at multiple levels:install()andmultisite_install()methods before callingdo_install()do_install()itself before loading WordPress files or callingWP_CLI::set_url()This layered approach ensures the correct values are set as early as possible, before WordPress has a chance to cache or use incorrect values.
Changes
install()method to set proper$_SERVERvariables before callingdo_install()multisite_install()method to set proper$_SERVERvariables before callingdo_install()do_install()method as well for defense in depthpathinfo()for secure file extension detectionUtils\trailingslashit()for safe path constructionTesting
example.com) and with paths (example.com/subdir)wp core installandwp core multisite-installSecurity
pathinfo()instead of string matching for robust file extension detectionUtils\trailingslashit()for safe path construction to prevent path traversal issuesOriginal prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.