Skip to content

Instantly share code, notes, and snippets.

@BeardedGinger
Created September 26, 2016 17:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BeardedGinger/bc8a8dd80aca493755e938dca45947be to your computer and use it in GitHub Desktop.
Save BeardedGinger/bc8a8dd80aca493755e938dca45947be to your computer and use it in GitHub Desktop.
<?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 trailing slash.
if ( ! defined( 'RP_SUBFOLDER' ) ) {
define( 'RP_SUBFOLDER', 'blog' );
}
if( ! defined( 'RP_FINAL_URL' ) ) {
define( 'RP_FINAL_URL', 'https://www.finalsite.com/' );
}
if( ! defined( 'RP_ADMIN_URL' ) ) {
define( 'RP_ADMIN_URL', 'http://adminurl.com' );
}
/**
* 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 ( ! lc_is_login_page() ) {
if ( defined( 'WP_SITEURL' ) && RP_ADMIN_URL === WP_SITEURL ) {
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'], RP_FINAL_URL . 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
*
* @param $canonical string
*/
function lc_persistent_canonical( $canonical ) {
$canonical = str_replace( network_site_url(), RP_FINAL_URL . RP_SUBFOLDER . '/', $canonical );
return $canonical;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment