Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Feb 4, 2026

Fix "core install" URL when wp-cli is executed from root filesystem

Resolves the issue where wp core install defines 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), the wp core install command sets incorrect home and siteurl values. Instead of using the URL provided via the --url parameter (e.g., https://website.domain.tld), it produces malformed URLs like https://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 during wp_install().

Solution

Before calling wp_install(), the fix sets $_SERVER['PHP_SELF'], $_SERVER['SCRIPT_NAME'], and $_SERVER['SCRIPT_FILENAME'] to match the URL path provided via the --url parameter instead of the WP-CLI executable location.

Changes

  • Modified do_install() method in Core_Command.php to set proper $_SERVER variables
  • Added comprehensive comments explaining the fix
  • Used pathinfo() for secure file extension detection
  • Used Utils\trailingslashit() for safe path construction
  • Removed unnecessary defined('ABSPATH') check
  • Added Behat test scenarios to verify correct URLs are set
  • Added test scenario that simulates the actual bug condition with --require file
  • All code passes PHPCS and PHPStan checks

Testing

  • Added test scenarios for URLs without paths (example.com) and with paths (example.com/subdir)
  • Added test scenario that simulates WP-CLI at root filesystem location using --require to set problematic $_SERVER values
  • Manual testing confirms the logic works for various URL patterns
  • The fix also applies to wp core multisite-install (which calls do_install())

Security

  • Used pathinfo() instead of string matching for robust file extension detection
  • Used Utils\trailingslashit() for safe path construction to prevent path traversal issues
  • All security checks passed
Original prompt

This section details on the original issue you should resolve

<issue_title>"core install" defines an incorrect home and siteurl if wpcli is executed from the root of the filesystem</issue_title>
<issue_description>## Bug Report

Describe the current, buggy behavior

"core install" defines an incorrect home and siteurl if wpcli is executed from the root of the filesystem.

Describe how other contributors can replicate this bug

Describe what you would expect as the correct outcome

Those values should be identical to the --url passed to the "core install" command and not depending on the path where the wpcli executable is located.

Let us know what environment you are running this on

wp-cli 2.12.0 on Debian 11 and PHP 8.4.

OS:	Linux 5.10.0-34-amd64 wp-cli/core-command#1 SMP Debian 5.10.234-1 (2025-02-24) x86_64
Shell:	/bin/bash
PHP binary:	/usr/bin/php8.4
PHP version:	8.4.14
php.ini used:	/etc/php/8.4/cli/php.ini
MySQL binary:	/usr/bin/mariadb
MySQL version:	mariadb  Ver 15.1 Distrib 10.5.29-MariaDB, for debian-linux-gnu (x86_64) using  EditLine wrapper
SQL modes:	
WP-CLI root dir:	phar://wp-cli.phar/vendor/wp-cli/wp-cli
WP-CLI vendor dir:	phar://wp-cli.phar/vendor
WP_CLI phar path:	phar:///wp
WP-CLI packages dir:	/home/www/.wp-cli/packages/
WP-CLI cache dir:	/home/www/.wp-cli/cache
WP-CLI global config:	
WP-CLI project config:	
WP-CLI version:	2.12.0

Provide additional context

I stumbled into this issue because i'm executing wpcli on a bubblewrap microcontainer where i was bind mounting the wpcli phar on the root of the virtual filesystem for convenience but the issue is not related to bubblewrap as it's happening even on the host OS.</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 3 commits February 4, 2026 16:18
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>
Copilot AI changed the title [WIP] Fix incorrect home and siteurl in core install command Fix incorrect home/siteurl when WP-CLI runs from root filesystem Feb 4, 2026
Copilot AI requested a review from swissspidy February 4, 2026 16:24
@swissspidy swissspidy requested a review from Copilot February 4, 2026 19:07
@codecov
Copy link

codecov bot commented Feb 4, 2026

Codecov Report

❌ Patch coverage is 81.81818% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/Core_Command.php 81.81% 2 Missing ⚠️

📢 Thoughts on this report? Let us know!

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 --url path.
  • Normalize URL paths to an index.php-style script path for WordPress’s install-time URL detection.
  • Add Behat coverage for installs with --url values 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

"core install" defines an incorrect home and siteurl if wpcli is executed from the root of the filesystem

2 participants