Skip to content

Instantly share code, notes, and snippets.

@onnimonni
Created November 8, 2016 07:07
Show Gist options
  • Save onnimonni/d6779c49b19d364dc62bf396b5565148 to your computer and use it in GitHub Desktop.
Save onnimonni/d6779c49b19d364dc62bf396b5565148 to your computer and use it in GitHub Desktop.
Redirects alias site wp-admin to original site admin when using mercator plugin https://github.com/humanmade/Mercator.
<?php
/**
* Plugin Name: Mercator https domain alias
* Description: Redirects mercator aliases into main site when wp-admin is accessed
* Version: 0.1
* Author: Onni Hakala / Geniem Oy
* Author URI: https://github.com/onnimonni
* License: MIT
*/
// If user wants to get into /wp-admin or /wp-login redirect them into the main domain
if ( substr( $_SERVER['REQUEST_URI'], 0, strlen( '/wp-admin' ) ) === '/wp-admin' ||
substr( $_SERVER['REQUEST_URI'], 0, strlen( '/wp-login' ) ) === '/wp-login'
) {
// Check if we are in mercator alias and redirect wp-admin and wp-login requests into real domain
$mapping = Mercator\Mapping::get_by_domain( $_SERVER['HTTP_HOST'] );
// If mapping was found redirect into the main domain
if ( $mapping !== null ) {
$redirect = get_site_url( $mapping->get_site_id(), $_SERVER['REQUEST_URI'], 'https' );
// Add some debugging information
header( "X-Site-Mapping: " . $mapping->get_site_id() );
// wp_redirect can't be used yet
header( "Location: {$redirect}" );
http_response_code( 302 );
die();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment