Skip to content

Instantly share code, notes, and snippets.

@morganestes
Last active November 27, 2023 17:35
Show Gist options
  • Save morganestes/3ba28668f94eea7faf68cd941394aeb9 to your computer and use it in GitHub Desktop.
Save morganestes/3ba28668f94eea7faf68cd941394aeb9 to your computer and use it in GitHub Desktop.
Sets WP constants in Lando sites
<?php
/**
* Sets WP_HOME and WP_SITEURL based on environment settings.
*
* @return void
*/
/**
* Sets WP_HOME and WP_SITEURL based on environment settings.
*
* @return void
*/
( function (): void {
$filter = fn( string $type ): ?string => filter_var(
getenv( $type, true ) ?: getenv( $type ),
FILTER_SANITIZE_URL
);
if (
defined( 'WP_HOME' ) && ! empty( $filter( 'WP_HOME' ) ) &&
defined( 'WP_SITEURL' ) && ! empty( $filter( 'WP_SITEURL' ) )
) {
return;
}
$home_url = $filter( 'WP_HOME' );
if ( getenv( 'PANTHEON_ENVIRONMENT' ) === 'lando' && empty( $home_url ) ) {
$home_url = filter_var(
sprintf( 'https://%1$s.%2$s', getenv( 'LANDO_APP_NAME' ), getenv( 'LANDO_DOMAIN' ) ),
FILTER_SANITIZE_URL
);
}
define( 'WP_HOME', $home_url );
if ( ! defined( 'WP_SITEURL' ) ) {
$site_url = $filter( 'WP_SITEURL' );
define( 'WP_SITEURL', empty( $site_url ) ? $home_url . '/wp' : $site_url );
}
} )();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment