Created
August 31, 2016 03:56
-
-
Save BeardedGinger/f4f97a3aec1ba0f39d88e715650cf192 to your computer and use it in GitHub Desktop.
public
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 | |
/** | |
* Plugin Name: Reverse Proxy for WordPress Multisite | |
* Plugin URI: https://limecuda.com/ | |
* Description: Reverse proxy setup for a WordPress multisite in subdirectory mode | |
* Version: 1.0.0 | |
* Author: Josh Mallard | |
* Author URI: https://limecuda.com | |
* | |
* @package reverse-proxy-wp-multisite | |
* | |
* Props to James Paden of https://instrumentalapp.com for the original code we based this off of. | |
* @link https://pressable.com/blog/2015/10/15/reverse-proxy-plugin-for-using-a-hosted-wordpress-site-in-a-subdirectory/ | |
*/ | |
// Change to match the desired subfolder, no leading or tralling slash. | |
define( 'RP_SUBFOLDER', 'blog' ); | |
/** | |
* Conditional to check if we're on the login page for the originating site | |
* | |
* @return boolean | |
* @since 1.0.0 | |
*/ | |
function lc_is_login_page() { | |
return in_array( $GLOBALS['pagenow'], array( 'wp-login.php', 'wp-register.php' ), true ); | |
} | |
/** | |
* If the request is coming from the Reverse Proxy, replace all URLs being output by WordPress | |
* with the final site URL | |
* | |
* @link http://stackoverflow.com/questions/772510/wordpress-filter-to-modify-final-html-output | |
*/ | |
if( ! is_admin() && ! lc_is_login_page() && $_GET['preview'] !== 'true' ) { | |
if ( isset( $_SERVER['HTTP_X_FORWARDED_SERVER'] ) ) { | |
ob_start(); | |
add_action('shutdown', function() { | |
$final = ''; | |
$levels = count( ob_get_level() ); | |
for ( $i = 0; $i < $levels; $i++ ) { | |
$final .= ob_get_clean(); | |
} | |
$final = str_replace( 'http://' . $_SERVER['HTTP_HOST'], 'https://' . $_SERVER['HTTP_X_FORWARDED_SERVER'] . '/' . RP_SUBFOLDER, $final ); | |
echo $final; | |
}, 0 ); | |
} | |
} | |
add_filter( 'wpseo_canonical', 'lc_persistent_canonical' ); | |
/** | |
* Make this a universal change since we want this applied everywhere | |
* even if we aren't viewing view Reverse Proxy. | |
* | |
* Replace the "final_url" with the correct Final URL for the site | |
*/ | |
function lc_persistent_canonical( $canonical ) { | |
$canonical = str_replace( get_bloginfo( 'url' ), 'final_url', $canonical ); | |
return $canonical; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment