dsebao
7/21/2017 - 2:52 PM

ngrok for wordpress localsites (making relative url)

ngrok for wordpress localsites (making relative url)

if (strstr($_SERVER['HTTP_HOST'], 'ngrok.io') !== false){
	
	function callback_relative_url($buffer) {
	  // Replace normal URLs
	  $home_url = esc_url(home_url('/'));
	  $home_url_relative = wp_make_link_relative($home_url);

	  // Replace URLs in inline scripts
	  $home_url_escaped = str_replace('/', '\/', $home_url);
	  $home_url_escaped_relative = str_replace('/', '\/', $home_url_relative);

	  $buffer = str_replace($home_url, $home_url_relative, $buffer);
	  $buffer = str_replace($home_url_escaped, $home_url_escaped_relative, $buffer);

	  return $buffer;
	}

	function buffer_start_relative_url() { ob_start('callback_relative_url'); }
	function buffer_end_relative_url() { ob_end_flush(); }

	// http://codex.wordpress.org/Plugin_API/Action_Reference
	add_action('registered_taxonomy', 'buffer_start_relative_url');
	add_action('shutdown', 'buffer_end_relative_url');
}