Skip to content

Instantly share code, notes, and snippets.

@felipelavinz
Last active July 18, 2016 09:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save felipelavinz/9063885 to your computer and use it in GitHub Desktop.
Save felipelavinz/9063885 to your computer and use it in GitHub Desktop.
WordPress Multitenancy configuration
<?php
// Create a copy of this file as local-config.php
// and define the required constants
define('DB_NAME', 'some_prefix_'. $requested_site);
define('DB_USER', 'somecleverusername');
define('DB_PASSWORD', 'someawesomesupersecretpassword');
define('DB_HOST', 'localhost');
define('DOMAIN_CURRENT_SITE', $requested_site .'.main-site.com' );
// If you're using the APC object cache backend, you can define a unique key such as
define('WP_APC_KEY_SALT', md5(DB_NAME . DOMAIN_CURRENT_SITE));
// Set debug constants if needed
/*
define('WP_DEBUG', true);
define('WP_DEBUG_DISPLAY', false);
define('WP_DEBUG_LOG', true);
define('SAVEQUERIES', true);
*/
<?php
/**
* Initialize info for the current site and blog
* This will let us share a single WordPress installation and store the uploads on separate directories according to the site
* This version of sunrise.php it's mostly copied from the one included on the Domain Mapping plugin and must be located on wp-content/sunrise.php
*/
if ( !isset( $current_site ) || !isset( $current_blog ) ) {
$domain = addslashes( $_SERVER['HTTP_HOST'] );
if ( false !== strpos( $domain, ':' ) ) {
if ( substr( $domain, -3 ) == ':80' ) {
$domain = substr( $domain, 0, -3 );
$_SERVER['HTTP_HOST'] = substr( $_SERVER['HTTP_HOST'], 0, -3 );
} elseif ( substr( $domain, -4 ) == ':443' ) {
$domain = substr( $domain, 0, -4 );
$_SERVER['HTTP_HOST'] = substr( $_SERVER['HTTP_HOST'], 0, -4 );
} else {
wp_load_translations_early();
wp_die( __( 'Multisite only works without the port number in the URL.' ) );
}
}
$domain = rtrim( $domain, '.' );
$cookie_domain = $domain;
if ( substr( $cookie_domain, 0, 4 ) == 'www.' )
$cookie_domain = substr( $cookie_domain, 4 );
$path = preg_replace( '|([a-z0-9-]+.php.*)|', '', $_SERVER['REQUEST_URI'] );
$path = str_replace ( '/wp-admin/', '/', $path );
$path = preg_replace( '|(/[a-z0-9-]+?/).*|', '$1', $path );
$current_site = wpmu_current_site();
if ( ! isset( $current_site->blog_id ) )
$current_site->blog_id = $wpdb->get_var( $wpdb->prepare( "SELECT blog_id FROM $wpdb->blogs WHERE domain = %s AND path = %s", $current_site->domain, $current_site->path ) );
if ( is_subdomain_install() ) {
$current_blog = wp_cache_get( 'current_blog_' . $domain, 'site-options' );
if ( !$current_blog ) {
$current_blog = get_blog_details( array( 'domain' => $domain ), false );
if ( $current_blog )
wp_cache_set( 'current_blog_' . $domain, $current_blog, 'site-options' );
}
if ( $current_blog && $current_blog->site_id != $current_site->id ) {
$current_site = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->site WHERE id = %d", $current_blog->site_id ) );
if ( ! isset( $current_site->blog_id ) )
$current_site->blog_id = $wpdb->get_var( $wpdb->prepare( "SELECT blog_id FROM $wpdb->blogs WHERE domain = %s AND path = %s", $current_site->domain, $current_site->path ) );
} else
$blogname = substr( $domain, 0, strpos( $domain, '.' ) );
} else {
$blogname = htmlspecialchars( substr( $_SERVER[ 'REQUEST_URI' ], strlen( $path ) ) );
if ( false !== strpos( $blogname, '/' ) )
$blogname = substr( $blogname, 0, strpos( $blogname, '/' ) );
if ( false !== strpos( $blogname, '?' ) )
$blogname = substr( $blogname, 0, strpos( $blogname, '?' ) );
$reserved_blognames = array( 'page', 'comments', 'blog', 'wp-admin', 'wp-includes', 'wp-content', 'files', 'feed' );
if ( $blogname != '' && ! in_array( $blogname, $reserved_blognames ) && ! is_file( $blogname ) )
$path .= $blogname . '/';
$current_blog = wp_cache_get( 'current_blog_' . $domain . $path, 'site-options' );
if ( ! $current_blog ) {
$current_blog = get_blog_details( array( 'domain' => $domain, 'path' => $path ), false );
if ( $current_blog )
wp_cache_set( 'current_blog_' . $domain . $path, $current_blog, 'site-options' );
}
unset($reserved_blognames);
}
if ( ! defined( 'WP_INSTALLING' ) && is_subdomain_install() && ! is_object( $current_blog ) ) {
if ( defined( 'NOBLOGREDIRECT' ) ) {
$destination = NOBLOGREDIRECT;
if ( '%siteurl%' == $destination )
$destination = "http://" . $current_site->domain . $current_site->path;
} else {
$destination = 'http://' . $current_site->domain . $current_site->path . 'wp-signup.php?new=' . str_replace( '.' . $current_site->domain, '', $domain );
}
header( 'Location: ' . $destination );
die();
}
if ( ! defined( 'WP_INSTALLING' ) ) {
if ( $current_site && ! $current_blog ) {
if ( $current_site->domain != $_SERVER[ 'HTTP_HOST' ] ) {
header( 'Location: http://' . $current_site->domain . $current_site->path );
exit;
}
$current_blog = get_blog_details( array( 'domain' => $current_site->domain, 'path' => $current_site->path ), false );
}
if ( ! $current_blog || ! $current_site )
ms_not_installed();
}
$blog_id = $current_blog->blog_id;
$public = $current_blog->public;
if ( empty( $current_blog->site_id ) )
$current_blog->site_id = 1;
$site_id = $current_blog->site_id;
$current_site = get_current_site_name( $current_site );
if ( ! $blog_id ) {
if ( defined( 'WP_INSTALLING' ) ) {
$current_blog->blog_id = $blog_id = 1;
} else {
wp_load_translations_early();
$msg = ! $wpdb->get_var( "SHOW TABLES LIKE '$wpdb->site'" ) ? ' ' . __( 'Database tables are missing.' ) : '';
wp_die( __( 'No site by that name on this system.' ) . $msg );
}
}
}
// On this point we already know which site and blog the user it's viewing
// Define this var to store and get uploads from the correct directory
define('BLOGUPLOADDIR', WP_CONTENT_DIR ."/uploads/{$requested_site}/blogs.dir/{$current_blog->blog_id}/files/");
<?php
// Get the name of the requested subdomain
$requested_site = current( explode('.', $_SERVER['HTTP_HOST']) );
// Create a list of existing sites as whitelist
$existing_sites = array(
'subdomain',
'othersubdomain',
'anothersubdomain'
);
// If the requested_site it's not whitelisted, redirect to some other site
if ( ! in_array($requested_site, $existing_sites) ) {
header('Location: http://www.main-site.com');
exit;
}
// Enable WordPress Multisite
define( 'WP_ALLOW_MULTISITE', true );
define( 'SUBDOMAIN_INSTALL', false );
$base = '/';
define( 'PATH_CURRENT_SITE', '/' );
define( 'SITE_ID_CURRENT_SITE', 1 );
define( 'BLOG_ID_CURRENT_SITE', 1 );
// Include wp-content/sunrise.php
// This it's needed to show uploaded images and files
define( 'SUNRISE', true );
// Define the default theme for a site (optional)
define('WP_DEFAULT_THEME', $requested_site .'-2013');
// Define local configuration constants
// Must define: DB_NAME; DB_USER; DB_PASSWORD; DB_HOST; DOMAIN_CURRENT_SITE
// Can get the requested site with the $requested_site variable
// Should also define WP_DEBUG-related constants
require __DIR__ .'/local-config.php';
// Some other DB constants
define('DB_CHARSET', 'utf8');
define('DB_COLLATE', '');
// Cryptographic salts (must be unique for our installation)
// Get yours at https://api.wordpress.org/secret-key/1.1/salt/
define('AUTH_KEY', 'Qd}A+xM5Q}/?W=4z!xj@TGR|vXX-|q=e`VESQ6L]wJ(vmupbehZ|omr|7AUD!fFv');
define('SECURE_AUTH_KEY', 'hoy,-yZZ.|vJ@ypncWUIDT$+bWHkq(l#kjFQ_&].A%/cwhD@Hz[~KU.L G>?TG2[');
define('LOGGED_IN_KEY', 'Z]l?L20l~PbM(xuAsKZa}UGLxot+p8QSW~TKe$.Ud)7QGDbMQY8dNsyoD}epo(5O');
define('NONCE_KEY', 'c)r^~orN{7F=+8#n`(+9}P:0RLM`TP|1+3B7^oz{hn%Qa49Ttl2&I<x JnWMebvI');
define('AUTH_SALT', '/J65qC5dIU+Zp:27Qp8S>!|xTEs5}Or!y|f5hU-@S:%m1`n+f%uPvl.QJ_Zv~=~h');
define('SECURE_AUTH_SALT', 'm6;S+<O+$@5ScxIX_D/~9g6p,eS|M(#:yJd(-!t>EZ[a`^Uv`omgI&,N5g`Rc]Ax');
define('LOGGED_IN_SALT', 'xTMJ|_81X+^2a-,%/_iL:&x$4uHLPJ7[1HkeW.ck&w9lS&%glvut}u{9Rz09-,^(');
define('NONCE_SALT', '|]&VF|(f;GW,-ml/$4_P1WJMDpzLZ5M+@z5 mSqOXZ V<aMRh?8-8~^c?7b2-y4v');
// Table prefix
$table_prefix = 'wp_somerandomstuff_';
// WordPress language
define('WPLANG', 'en_US');
// --------------------------------------------
// Any other constants must defined BEFORE this
/** WordPress absolute path to the Wordpress directory. */
if ( !defined('ABSPATH') )
define('ABSPATH', dirname(__FILE__) . '/');
/** Sets up WordPress vars and included files. */
require_once(ABSPATH . 'wp-settings.php');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment