roots / multisite-url-fixer

Fixes WordPress issues with home and site URL on multisite when using Bedrock
MIT License
59 stars 19 forks source link

Subdomain based multisite installation frontend URLs #3

Closed benjaminniess closed 5 years ago

benjaminniess commented 6 years ago

Hi,

First of all thanks for your really helpful tool!

I'm running into an annoying problem with URLs in a subdomain based multisite WP install. On the main site, no problem because it's the root URL but on all other sites, all dynamic URLs (based on the "content_url()" function) are wrong because they're based on root domain instead of subdomain.

All assets for example have a URL like

http://maindomain/wp-content-dir/asset

instead of

http://subdomain.maindomain/wp-content-dir/asset

This is due to the definition of the WP_HOME constant with a unique domain.

I've done a fix based on the 'content_url' filter

<?php
add_filter('content_url', 'wp_subdomain_url_fixer', 10, 2);
function wp_subdomain_url_fixer($url, $path)
{
    $network = get_network();
    $current_site = get_blog_details();

    // Security replace subdomain by domain
    // (should never do anything but will avoid subdomain.subdomain.domain if it does)
    $url = str_replace($current_site->domain, $network->domain, $url);

    // Replace domain.com by subdomain.domain.com
    $url = str_replace($network->domain, $current_site->domain, $url);

    return $url;
}

Please let me know what you think about it and feel free to incorporate it in your url fixer addon.

Thanks.

JulienMelissas commented 6 years ago

Hey there @benjaminniess.

I can confirm I'm seeing this as well. I didn't notice it because in production I don't use the default WP image stuff.

I think this might be default WP behavior, and not related to Bedrock. Will test it out, but if that's the case, I don't think I'd want to include this code, although it is helpful and I'd recommend you throw it up on GitHub or something like that.

JulienMelissas commented 5 years ago

Related: https://github.com/roots/bedrock/pull/216 and the discussion over at https://github.com/roots/bedrock/issues/325

Thanks again to @benjaminniess for documenting their fix. I would also recommend checking out the solution on the above referenced issues.

Closing for now as this seems to be default WP behavior and out of scope of this plugin.