Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions features/core-install.feature
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,87 @@ Feature: Install WordPress core
"""
And the return code should be 0

Scenario: Verify correct siteurl and home when URL has no path
Given an empty directory
And WP files
And wp-config.php
And a database

When I run `wp core install --url=example.com --title=Test --admin_user=wpcli --admin_email=wpcli@example.org --admin_password=password --skip-email`
Then STDOUT should contain:
"""
Success: WordPress installed successfully.
"""

When I run `wp option get home`
Then STDOUT should be:
"""
http://example.com
"""

When I run `wp option get siteurl`
Then STDOUT should be:
"""
http://example.com
"""

Scenario: Verify correct siteurl and home when URL has a path
Given an empty directory
And WP files
And wp-config.php
And a database

When I run `wp core install --url=example.com/subdir --title=Test --admin_user=wpcli --admin_email=wpcli@example.org --admin_password=password --skip-email`
Then STDOUT should contain:
"""
Success: WordPress installed successfully.
"""

When I run `wp option get home`
Then STDOUT should be:
"""
http://example.com/subdir
"""

When I run `wp option get siteurl`
Then STDOUT should be:
"""
http://example.com/subdir
"""

Scenario: Install with simulated root filesystem WP-CLI executable location
Given an empty directory
And WP files
And wp-config.php
And a database
And a problematic-server-vars.php file:
"""
<?php
// Simulate WP-CLI being executed from root filesystem (e.g., /wp)
// This is the problematic scenario that the fix addresses
$_SERVER['PHP_SELF'] = '/wp';
$_SERVER['SCRIPT_NAME'] = '/wp';
$_SERVER['SCRIPT_FILENAME'] = '/wp';
"""

When I run `wp core install --url=https://example.com --title=Test --admin_user=wpcli --admin_email=wpcli@example.org --admin_password=password --skip-email --require=problematic-server-vars.php`
Then STDOUT should contain:
"""
Success: WordPress installed successfully.
"""

When I run `wp option get home`
Then STDOUT should be:
"""
https://example.com
"""

When I run `wp option get siteurl`
Then STDOUT should be:
"""
https://example.com
"""

@less-than-php-7
Scenario: Install WordPress with locale set to de_DE on WP < 4.0
Given an empty directory
Expand Down
29 changes: 29 additions & 0 deletions src/Core_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,35 @@ function wp_new_blog_notification() {
// which is normally a runtime argument
if ( isset( $assoc_args['url'] ) ) {
WP_CLI::set_url( $assoc_args['url'] );

// Fix $_SERVER['PHP_SELF'] and $_SERVER['SCRIPT_NAME'] to prevent incorrect
// URL detection by WordPress during installation. When WP-CLI is executed from
// the root of the filesystem (e.g., /wp), these variables contain the WP-CLI
// executable path rather than the WordPress installation path, which causes
// wp_guess_url() to construct incorrect URLs.
$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.
// If the path doesn't already end with a file (no extension in basename),
// append '/index.php' to represent the WordPress entry point.
$path = rtrim( $path, '/' );
if ( empty( $path ) ) {
$path = '/index.php';
} elseif ( '' === pathinfo( $path, PATHINFO_EXTENSION ) ) {
// Path doesn't end with a file, append /index.php
$path .= '/index.php';
}

$_SERVER['PHP_SELF'] = $path;
$_SERVER['SCRIPT_NAME'] = $path;

// Set SCRIPT_FILENAME to the actual WordPress index.php if available.
// This is optional and only set when ABSPATH is defined and index.php exists.
// If not set, WordPress can still function using PHP_SELF and SCRIPT_NAME.
if ( file_exists( Utils\trailingslashit( ABSPATH ) . 'index.php' ) ) {
$_SERVER['SCRIPT_FILENAME'] = Utils\trailingslashit( ABSPATH ) . 'index.php';
}
}

$public = true;
Expand Down
Loading