Last active
December 11, 2023 16:27
-
-
Save cr0ybot/c7321f0ed3534d0371bbac6722689d2a to your computer and use it in GitHub Desktop.
WordPress Multisite Local/Staging/Live Dev Setup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* File wp-content/sunrise.php | |
* | |
* This allows us to copy the production multisite database to staging/local and still use | |
* it directly without altering domains | |
* | |
* This file can be present on all copies of the site, but it should only be added to the | |
* wp-config file of local/staging (via `define( 'SUNRISE', true )`) | |
* | |
* @author Cory Hughart <[email protected]> | |
*/ | |
/** | |
* Filter /wp-includes/ms-load.php::get_site_by_path() | |
* and /wp-includes/class-wp-network.php::get_by_path() | |
**/ | |
function dev_get_site_by_path( $_site, $_domain, $_path, $_segments, $_paths ) { | |
global $wpdb, $path; | |
// Get our actual domain in the database (should be set to production domain) | |
// The domain coming in should be the request domain | |
$domain = str_replace( WP_DEV_TLD, WP_PROD_TLD, $_domain); | |
// Search for a site matching the domain and first path segment | |
$site = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s and path = %s", $domain, $_paths[0] ) ); | |
$current_path = $_paths[0]; | |
if ($site === null) { | |
// Specifically for the main blog - if a site is not found then load the main blog | |
$site = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s and path = %s", $domain, '/' ) ); | |
$current_path = '/'; | |
} | |
// Set path to match the first segment | |
$path = $current_path; | |
return $site; | |
} | |
add_filter( 'pre_get_site_by_path', 'dev_get_site_by_path', 1, 5 ); | |
add_filter( 'pre_get_network_by_path', 'dev_get_site_by_path', 1, 5 ); | |
/** | |
* Filter the site_url and home options for each site, and | |
* filter /wp-includes/link-template.php::network_site_url() | |
* and /wp-includes/link-template.php::network_home_url() | |
* so that our network site link is correct in the admin menu | |
*/ | |
function dev_network_url( $_url = '' ) { | |
return str_replace( WP_PROD_TLD, WP_DEV_TLD, $_url ); | |
} | |
add_filter( 'network_site_url', 'dev_network_url' ); | |
add_filter( 'network_home_url', 'dev_network_url' ); | |
add_filter( 'option_siteurl', 'dev_network_url' ); | |
add_filter( 'option_home', 'dev_network_url' ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Excerpt fron LIVE wp-config | |
*/ | |
// ... | |
define('WP_DEBUG', false); | |
/** Set this to your LIVE tld setup */ | |
define( 'WP_HOME', 'https://mysite.com'); | |
define( 'WP_SITEURL', 'https://mysite.com'); | |
/** Do NOT include wp-content/sunrise.php */ | |
// define( 'SUNRISE', false ); | |
/** This should be the TLD in the database */ | |
// define( 'WP_PROD_TLD', '.com' ); | |
/** This should be the tld of this server */ | |
// define( 'WP_DEV_TLD', '.stagingsite.com'); | |
/** Multisite config */ | |
define( 'WP_ALLOW_MULTISITE', true ); | |
define( 'MULTISITE', true ); | |
define( 'SUBDOMAIN_INSTALL', true ); // false if sub-folder install | |
define( 'DOMAIN_CURRENT_SITE', 'mysite.com' ); // LIVE domain | |
define( 'PATH_CURRENT_SITE', '/' ); | |
define( 'SITE_ID_CURRENT_SITE', 1 ); | |
define( 'BLOG_ID_CURRENT_SITE', 1 ); | |
define('COOKIE_DOMAIN', $_SERVER['HTTP_HOST']); | |
//define('WP_DEFAULT_THEME', 'default'); // apply a theme by default to all newly created sites | |
// ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Excerpt fron LOCAL wp-config | |
*/ | |
// ... | |
define('WP_DEBUG', true); | |
/** Set this to your LOCAL tld setup */ | |
define( 'WP_HOME', 'http://mysite.local'); | |
define( 'WP_SITEURL', 'http://mysite.local'); | |
/** Include wp-content/sunrise.php */ | |
define( 'SUNRISE', true ); | |
/** This should be the TLD in the database */ | |
define( 'WP_PROD_TLD', '.com' ); | |
/** This should be the tld of this server */ | |
define( 'WP_DEV_TLD', '.local'); | |
/** Multisite config */ | |
define( 'WP_ALLOW_MULTISITE', true ); | |
define( 'MULTISITE', true ); | |
define( 'SUBDOMAIN_INSTALL', true ); // false if sub-folder install | |
define( 'DOMAIN_CURRENT_SITE', 'mysite.com' ); // LIVE domain | |
define( 'PATH_CURRENT_SITE', '/' ); | |
define( 'SITE_ID_CURRENT_SITE', 1 ); | |
define( 'BLOG_ID_CURRENT_SITE', 1 ); | |
define('COOKIE_DOMAIN', $_SERVER['HTTP_HOST']); | |
//define('WP_DEFAULT_THEME', 'default'); // apply a theme by default to all newly created sites | |
// ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Excerpt fron STAGING wp-config | |
*/ | |
// ... | |
define('WP_DEBUG', true); | |
/** Set this to your STAGING tld setup */ | |
define( 'WP_HOME', 'http://mysite.stagingserver.com'); | |
define( 'WP_SITEURL', 'http://mysite.stagingserver.com'); | |
/** Include wp-content/sunrise.php */ | |
define( 'SUNRISE', true ); | |
/** This should be the TLD in the database */ | |
define( 'WP_PROD_TLD', '.com' ); | |
/** This should be the tld of this server */ | |
define( 'WP_DEV_TLD', '.stagingserver.com'); | |
/** Multisite config */ | |
define( 'WP_ALLOW_MULTISITE', true ); | |
define( 'MULTISITE', true ); | |
define( 'SUBDOMAIN_INSTALL', true ); // false if sub-folder install | |
define( 'DOMAIN_CURRENT_SITE', 'mysite.com' ); // LIVE domain | |
define( 'PATH_CURRENT_SITE', '/' ); | |
define( 'SITE_ID_CURRENT_SITE', 1 ); | |
define( 'BLOG_ID_CURRENT_SITE', 1 ); | |
define('COOKIE_DOMAIN', $_SERVER['HTTP_HOST']); | |
//define('WP_DEFAULT_THEME', 'default'); // apply a theme by default to all newly created sites | |
// ... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment