Make WordPress Core

Opened 13 years ago

Last modified 3 weeks ago

#27287 new defect (bug)

siteurl is missing WordPress path when creating a new site

Reported by: danielbachhuber Owned by:
Priority: normal Milestone:
Component: Networks and Sites Version:
Severity: normal Keywords: has-patch has-unit-tests
Cc: Focuses: multisite

Description

Our setup is a single subdomain network. WordPress is installed to /wp, and we don't have rewrites configured to mask the subdirectory.

On the initial install, wp_install() sets the value of siteurlbased on wp_guess_url(). The main site ends up with these expected values:

  • siteurl: domain.com/wp
  • home: domain.com

On subsequent creation of a new site, install_blog() incorrectly sets the option values for the new site to:

  • siteurl: domain.com
  • home: domain.com

This means the admin loads, but CSS and resources are broken, until I update the siteurl option value to be "domain.com/wp".

I'd expect install_blog() to follow the same behavior as wp_install_blog().

#23221 is related, although this suggested fix doesn't also fix that.

Change History (8)

#1 @thewebists
13 years ago

I've come across this issue when using Bedrock (and the same would apply to Mark Jaquith's Skeleton).

For those seeing this problem, my quick fix for this is to define a CORE_DIR constant in wp-config to specify an optional subdirectory location for the core WordPress files. Then I run the following in a mu-plugin:

/**
 * Temporary fix for issue #27287 for multisite blogs with WordPress in it's own subdirectory
 * for use with projects such as https://github.com/roots/bedrock and 
 * https://github.com/markjaquith/WordPress-Skeleton
 * 
 * @see https://core.trac.wordpress.org/ticket/27287
 */
add_action( 'wpmu_new_blog', function( $blog_id ){
	if (defined('SUBDOMAIN_INSTALL') && SUBDOMAIN_INSTALL && defined('CORE_DIR') && !empty(CORE_DIR)) {
		switch_to_blog($blog_id);
		$siteurl = untrailingslashit(get_option('siteurl'));
		if (!(strpos($siteurl, CORE_DIR) + strlen(CORE_DIR) === strlen($siteurl))) {
			update_option( 'siteurl', $siteurl . '/' . CORE_DIR );
		}
		restore_current_blog();
	}
});

If you're seeing this, then you've probably also come across issue #23221. See the OP's solution here: https://core.trac.wordpress.org/ticket/23221#comment:27

#2 @Compute
12 years ago

Adding a filter for the meta wpmu_create_blog https://core.trac.wordpress.org/ticket/28540 would make this task even easier.

Just adding:

$meta['siteurl'] = esc_url( $domain . '/wp );

To this filter would do the trick.

Version 0, edited 12 years ago by Compute (next)

#3 @chriscct7
11 years ago

  • Keywords needs-unit-tests added

#4 @flixos90
11 years ago

I use the following to ensure that all home / site URLs work properly (given my WordPress directory is called core, but a constant could obviously be used as well):

<?php
class URLFixer {
        public function run() {
                add_filter( 'option_home', array( $this, 'fix_home_url' ) );
                add_filter( 'sanitize_option_home', array( $this, 'fix_home_url' ) );

                add_filter( 'option_siteurl', array( $this, 'fix_site_url' ) );
                add_filter( 'sanitize_option_siteurl', array( $this, 'fix_site_url' ) );
        }

        public function fix_home_url( $value ) {
                if ( '/core' === substr( $value, -5 ) ) {
                        $value = substr( $value, 0, -5 );
                }
                return $value;
        }

        public function fix_site_url( $value ) {
                if ( '/core' !== substr( $value, -5 ) ) {
                        $value .= '/core';
                }
                return $value;
        }
}

There are still quite a few areas on multisite which assume that home and siteurl have the same value or that show one of the two values in the wrong context (see #35632). I wonder if we should create a centralized ticket for these issues.

This ticket was mentioned in ​Slack in #core-multisite by flixos90. ​View the logs.


11 years ago

#6 @realloc
3 months ago

Current state

install_blog() was deprecated in 5.1.0. Site initialization now runs through wp_initialize_site() in wp-includes/ms-site.php:

'home'    => untrailingslashit( $home_scheme . '://' . $site->domain . $site->path ),
'siteurl' => untrailingslashit( $siteurl_scheme . '://' . $site->domain . $site->path ),

Both values come from the same source. The only distinction is the scheme, and only for subdirectory installs. A separate directory for the WordPress files is still not accounted for.

Why there is no patch

The information does not exist anywhere the function could read it. The blogs table stores domain and path, and no option records that core lives in a subdirectory. That detail is determined once by wp_guess_url() during the initial install and ends up only in the main site's siteurl. A fix would have to reconstruct it from the difference between home and siteurl of the main site, or store it explicitly.

Current workaround

The approaches suggested above can be replaced. Since 5.1.0, wp_initialize_site_args allows setting $args['options']['siteurl'] before populate_options() runs, so no post-creation correction is needed. Installs defining WP_SITEURL and WP_HOME in wp-config.php are unaffected, since constants take precedence.

Open question

Should multisite support home and siteurl pointing to different locations at all? Several places assume they are interchangeable, see comment 4 and #35632. Answering that decides whether this is a small fix in wp_initialize_site() or part of a larger effort.

This ticket was mentioned in ​PR #14409 on ​WordPress/wordpress-develop by ​@abhishekfdd.


3 weeks ago
#7

  • Keywords has-patch has-unit-tests added; needs-patch needs-unit-tests removed

When WordPress is installed in its own subdirectory (for example, core in /wp/ with the network served from /) on a subdomain network, new sites are created with a siteurl of just the domain, such as http://site.example.org. The core directory is dropped, so wp-admin, wp-includes and admin assets are requested from a path that doesn't exist. Things only appear to work when extra rewrite rules mask the problem.

wp_initialize_site() now reads the path from the network's siteurl, takes the part relative to the network path (for example wp or app/wp), and appends it to the new site's siteurl. So a network siteurl of http://example.org/wp/ produces http://site.example.org/wp instead of http://site.example.org. home is unchanged.

Why subdirectory installs are excluded

On subdirectory networks, the rewrite rules generated during network setup in src/wp-admin/includes/network.php already route each site's wp-admin, wp-includes and wp-content requests, and its *.php files, to the core directory:

RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) {$rewrite_base}$2 [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ {$rewrite_base}$2 [L]

$rewrite_base there is the core subdirectory. So /site/wp-admin/ already resolves correctly, and appending the core directory to the site siteurl would change URLs that work today. In subdomain mode $subdir_match is empty, so these rules don't cover the per-site case, and the siteurl has to point at the real core location.

Testing

Added tests to tests/phpunit/tests/multisite/site.php:

  • test_wp_initialize_site_siteurl_subdomain (data provider: core in network root, core in a subdirectory, core in a nested subdirectory, no trailing slash). It runs only in subdomain mode.
  • test_wp_initialize_site_siteurl_subdirectory_ignores_core_directory. It runs only in subdirectory mode and checks that the core directory is not appended.

Note: core CI runs the multisite suite in subdirectory mode only, so the main subdomain test is skipped there. To run it locally, temporarily add this line to wp-tests-config.php above the ABSPATH define. The WP_INSTALLING guard keeps the test installer working.

if ( ! defined( 'WP_INSTALLING' ) ) { define( 'SUBDOMAIN_INSTALL', true ); }

Then run:

npm run test:php -- -c tests/phpunit/multisite.xml --filter 'siteurl_subdomain|ignores_core_directory'

Without the fix (subdomain mode, src/wp-includes/ms-site.php reverted, --filter 'siteurl_subdomain'):

.FFF                                                                4 / 4 (100%)

There were 3 failures:

1) Tests_Multisite_Site::test_wp_initialize_site_siteurl_subdomain with data set "core in subdirectory" ('http://example.org/wp/', 'http://ticket27287.example.org/wp')
Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
-'http://ticket27287.example.org/wp'
+'http://ticket27287.example.org'

2) Tests_Multisite_Site::test_wp_initialize_site_siteurl_subdomain with data set "core in nested subdir" ('http://example.org/app/wp/', 'http://ticket27287.example.org/app/wp')
Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
-'http://ticket27287.example.org/app/wp'
+'http://ticket27287.example.org'

3) Tests_Multisite_Site::test_wp_initialize_site_siteurl_subdomain with data set "no trailing slash" ('http://example.org/wp', 'http://ticket27287.example.org/wp')
Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
-'http://ticket27287.example.org/wp'
+'http://ticket27287.example.org'

FAILURES!
Tests: 4, Assertions: 12, Failures: 3.

With the fix (subdomain mode):

....S                                                               5 / 5 (100%)

There was 1 skipped test:

1) Tests_Multisite_Site::test_wp_initialize_site_siteurl_subdirectory_ignores_core_directory
This test is only valid in a subdirectory configuration.

OK, but incomplete, skipped, or risky tests!
Tests: 5, Assertions: 12, Skipped: 1.

In subdirectory mode (the default), --filter 'siteurl_subdomain|ignores_core_directory|test_new_blog_url_schemes|test_wp_initialize_site' gives 19 tests, 15 passing and the 4 subdomain cases skipped. The existing test_new_blog_url_schemes and test_wp_initialize_site* tests are unaffected.

Use of AI Tools

AI assistance: Yes
Tool: Claudecode
Model(s): Opus 5.5
Used for: Initial code skeleton and test suggestions; final implementation and tests were reviewed and edited by me.

#8 @abhishekfdd
3 weeks ago

Confirmed this still reproduces on trunk (7.2-alpha). Setup: core in /wp, subdomain network, main site with home = http://example.test and siteurl = http://example.test/wp. A new site created through wpmu_create_blog() or wp_insert_site() gets siteurl = http://sub.example.test, so admin_url() points at /wp-admin/, which 404s.

The ticket predates 5.1. The code now lives in wp_initialize_site(), which builds siteurl from $site->domain . $site->path and only reads the scheme from the network siteurl, so the core directory gets dropped.

PR: ​https://github.com/WordPress/wordpress-develop/pull/14409

Approach: for subdomain installs only, take the network siteurl path relative to the network path (for example wp from example.com/wp/) and append it to the new site's siteurl. home is unchanged. Values passed through wp_initialize_site_args still take precedence.

Subdirectory installs are intentionally excluded. The rewrite rules that network setup generates (wp-admin/includes/network.php) already route /site/wp-admin/ to the core directory, and appending /wp there would break under those rules. For subdomain installs, the appended path is a real directory, so it works with or without rewrites.

Tests: added a data provider test covering core in the network root, /wp, nested /app/wp, and a network siteurl without a trailing slash, plus a guard test that subdirectory installs are unchanged.

Note: core CI runs multisite in subdirectory mode only, so the subdomain test shows as skipped there. I verified it locally with SUBDOMAIN_INSTALL defined at runtime:

  • Without the fix: 3 of 4 cases fail (subdirectory, nested subdirectory, no trailing slash).
  • With the fix: all 4 pass.
  • Full multisite suite, patched vs unpatched trunk in both modes: identical failure sets, no regressions.

Would it be worth adding a subdomain multisite job to CI? Related multisite home/siteurl issues: #23221, #35632.

AI disclosure: Claude was used to help reproduce, draft, and test this patch. I reviewed and verified the change and test results; details are in the PR.

Note: See TracTickets for help on using tickets.