Setting WP_HOME and WP_SITEURL in application.php

I’m running a subdomain Bedrock installation, deployed with Trellis. I am using WP’s built-in domain mapping, not a plugin like Mercator. It works super well.

The issue I have is that wp’s media URLs are presently being constructed based on the parent site’s WP_HOME value – as defined in application.php and its .env vars. So client.org’s media is served from agency.org’s URL.

These resolve okay but it’s still not 100% desirable, and additionally I’ve bumped into a couple other systemic problems like cross-origin issues with SVGs and configuring CDNs to account for both client.org and agency.org.

I can pretty much fix this issue for myself utilizing the following definitions in application.php:

define('WP_HOME', 'https://' . $_SERVER['HTTP_HOST']); 
define('WP_SITEURL', 'https://'. $_SERVER['HTTP_HOST'] .'/wp');

Seems okay on staging! And works on the same principle as my fix for cookies issues, which I haven’t noticed any fallout from (yet):

define('COOKIE_DOMAIN', $_SERVER['HTTP_HOST']);

But, the things I don’t know could fill a codex…

So, yeah, am I about to make a huge mistake? What are the potential ramifications of my foolishness?

Is there a better way that can be explained in a manner understandable to a self-taught-designer-full-stack-wannabe like myself? If so, thank you.

For any future Bedrock noobs

I’ve been running this for a few days and everything seems fine.

1 Like

Thanks, it has been very useful.
To prevent PHP notices (e.g. with wp-cli) I added a fallback:

define('WP_HOME', isset($_SERVER['HTTP_HOST']) ? 'https://' . $_SERVER['HTTP_HOST'] : env('WP_HOME'));
define('WP_SITEURL', isset($_SERVER['HTTP_HOST']) ? 'https://' . $_SERVER['HTTP_HOST'] . '/wp' : env('WP_SITEURL'));

define('COOKIE_DOMAIN', isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '');