Skip to content

Instantly share code, notes, and snippets.

@chrono-meter
Created October 26, 2023 00:39
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 chrono-meter/08fd5388e30b251295008976691ab4ef to your computer and use it in GitHub Desktop.
Save chrono-meter/08fd5388e30b251295008976691ab4ef to your computer and use it in GitHub Desktop.
Spoofing host and port for WordPress environment
<?php
/**
* Spoofing host and port for WordPress environment.
* To use this, include from your "wp-config.php" file.
* Note that this functions is not intended to be used in production.
*
* @see \is_ssl()
* @link https://ngrok.com/docs/using-ngrok-with/wordpress/
*/
if ( ! empty( $host = @$_SERVER['HTTP_HOST'] ) ) {
$scheme = ! empty( $_SERVER['HTTPS'] ) ? 'https' : ( $_SERVER['REQUEST_SCHEME'] ?? 'http' );
if ( isset( $_SERVER['HTTP_X_FORWARDED_PROTO'] ) || isset( $_SERVER['HTTP_X_SCHEME'] )) {
// reverse proxy environment
$scheme = $_SERVER['HTTP_X_FORWARDED_PROTO'] ?? $_SERVER['HTTP_X_SCHEME'];
if ( $scheme === 'https' && $_SERVER['REQUEST_SCHEME'] === 'http' ) {
// faking https environment for WordPress generated URLs
define( 'FORCE_SSL_ADMIN', false );
$_SERVER['HTTPS'] = 'on';
}
} else if (
@$_SERVER['SERVER_PORT'] != ( $scheme === 'https' ? 443 : 80 )
&&
! str_contains( $host, ':' )
) {
// non-standard port
$host .= ':' . $_SERVER['SERVER_PORT'];
}
define( 'WP_SITEURL', $scheme . '://' . $host );
define( 'WP_HOME', WP_SITEURL );
defined( 'FORCE_SSL_ADMIN' ) || define( 'FORCE_SSL_ADMIN', $scheme === 'https' );
define( 'COOKIE_DOMAIN', $host );
define( 'SITECOOKIEPATH', '.' );
unset( $host, $scheme );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment