Skip to content

Instantly share code, notes, and snippets.

@felipeelia
Last active September 1, 2022 23:41
Show Gist options
  • Save felipeelia/3821646676923a46c6eb68c65a4dd750 to your computer and use it in GitHub Desktop.
Save felipeelia/3821646676923a46c6eb68c65a4dd750 to your computer and use it in GitHub Desktop.
WordPress MU Plugin to have it working with ngrok.io
<?php
/**
* Place this file in the wp-content/mu-plugins directory and run ngrok with
* `ngrok http http://<local_url> --host-header=<local_url>`
*/
$ngrok_url = '<id>.ngrok.io';
define( 'WP_HOME', 'http://' . $ngrok_url );
define( 'WP_SITEURL', 'http://' . $ngrok_url );
function callback( $buffer ) {
global $ngrok_url;
$buffer = str_replace( '<your_local_url>', $ngrok_url, $buffer );
return $buffer;
}
function buffer_start() {
ob_start( 'callback' );
}
function buffer_end() {
ob_end_flush();
}
add_action( 'wp_loaded', 'buffer_start' );
add_action( 'shutdown', 'buffer_end' );
@Rahmon
Copy link

Rahmon commented Apr 7, 2021

Small fix in callback function:

function callback( $buffer ) {
        global $ngrok_url;

	$buffer = str_replace( '<your_local_url>', $ngrok_url, $buffer );
	return $buffer;
}

@felipeelia
Copy link
Author

Updated. Thanks @Rahmon!

@dinhtungdu
Copy link

@felipeelia Thanks so much for this! I notice a minor issue in the example command, the host header should be --host-header=<local_url>. Otherwise, we will get the following error running that command:

ERROR: unknown shorthand flag: 'o' in -ost-header=oss.test

@dinhtungdu
Copy link

For ClassifAI, we will need this line to proxy the image URLs that sent to Computer Vision

add_filter( 'wp_get_attachment_url', 'callback' );

@felipeelia
Copy link
Author

I've updated the --host-header parameter in the example @dinhtungdu, thank you very much! Do you mind explaining a bit further why that line was needed for ClassifAI? Just so we can add a comment for people with similar problems.

@dinhtungdu
Copy link

Do you mind explaining a bit further why that line was needed for ClassifAI?

@felipeelia For ClassifAI, we need to proxy the image file URL because it's required by Computer Vision (which we use to process the images). We don't send the image file but the absolute URL to the API.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment