Skip to content

Instantly share code, notes, and snippets.

@timiwahalahti
Created November 19, 2020 16:28
Show Gist options
  • Save timiwahalahti/b98f2d732d62c4b927f751b6afbd1c1c to your computer and use it in GitHub Desktop.
Save timiwahalahti/b98f2d732d62c4b927f751b6afbd1c1c to your computer and use it in GitHub Desktop.
WordPress Network sunrise.php with shared development and staging environment databse
<?php
if ( defined( 'DOMAIN_CURRENT_SITE' ) && defined( 'PATH_CURRENT_SITE' ) ) {
$current_site = new stdClass;
$current_site->id = defined( 'SITE_ID_CURRENT_SITE' ) ? SITE_ID_CURRENT_SITE : 1;
$current_site->domain = $domain = DOMAIN_CURRENT_SITE;
$current_site->path = $path = PATH_CURRENT_SITE;
if ( defined( 'BLOG_ID_CURRENT_SITE' ) ) {
$current_site->blog_id = BLOG_ID_CURRENT_SITE;
} elseif ( defined( 'BLOGID_CURRENT_SITE' ) ) { // deprecated.
$current_site->blog_id = BLOGID_CURRENT_SITE;
}
// Fix the domain for shared dev and stage
if ( 'production' !== getenv( 'WP_ENV' ) ) {
$domain = str_replace( DOMAIN_CURRENT_SITE, 'project.test', $domain );
}
$url = parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH );
$patharray = (array) explode( '/', trim( $url, '/' ) );
$pathsearch = '';
$blogsearch = '';
if ( count( $patharray ) ) {
foreach ( $patharray as $pathpart ) {
$pathsearch .= '/' . $pathpart;
$blogsearch .= $wpdb->prepare( " OR (domain = %s AND path = %s) ", $domain, $pathsearch . '/' );
}
}
$current_blog = $wpdb->get_row( $wpdb->prepare( "SELECT *, LENGTH( path ) as pathlen FROM $wpdb->blogs WHERE domain = %s AND path = '/'", $domain ) . $blogsearch . 'ORDER BY pathlen DESC LIMIT 1' );
$blog_id = $current_blog->blog_id;
$public = $current_blog->public;
$site_id = $current_blog->site_id;
$current_site = _ss_get_current_site_name( $current_site );
}
function _ss_get_current_site_name( $current_site ) {
global $wpdb;
$current_site->site_name = wp_cache_get( $current_site->id . ':current_site_name', 'site-options' );
if ( ! $current_site->site_name ) {
$current_site->site_name = $wpdb->get_var( $wpdb->prepare( "SELECT meta_value FROM $wpdb->sitemeta WHERE site_id = %d AND meta_key = 'site_name'", $current_site->id ) );
if ( null == $current_site->site_name ) {
$current_site->site_name = ucfirst( $current_site->domain );
}
wp_cache_set( $current_site->id . ':current_site_name', $current_site->site_name, 'site-options' );
}
return $current_site;
}
function _ss_fix_dev_stage_url( $url ) {
return str_replace( [ 'project.test', 'project.vaiheessa.fi' ], DOMAIN_CURRENT_SITE, $url );
}
// Fix the domain for shared dev and stage
if ( 'production' !== getenv( 'WP_ENV' ) ) {
add_filter( 'get_site', function( $site ) {
$site->domain = DOMAIN_CURRENT_SITE;
return $site;
}, 1, 1 );
add_filter( 'network_home_url', '_ss_fix_dev_stage_url', 1, 1 );
add_filter( 'home_url', '_ss_fix_dev_stage_url', 1, 1 );
add_filter( 'option_home_url', '_ss_fix_dev_stage_url', 1, 1 );
add_filter( 'option_siteurl', '_ss_fix_dev_stage_url', 1, 1 );
add_filter( 'network_site_url', '_ss_fix_dev_stage_url', 1, 1 );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment