Skip to content

Hack for working locally with APIs

Eason edited this page Jun 13, 2025 · 6 revisions
// Filter the WC API
add_filter( 'determine_current_user', function( $user_id ) {
	return 1;
}, 20 );
add_filter( 'woocommerce_api_check_authentication', function( $user ) {
	return new WP_User( 1 );
} );
add_filter( 'rest_authentication_errors', '__return_null', 500 );

Use Ads account without setting billing data

On your site you'll want to have the code snippet:

add_filter(
	'woocommerce_gla_ads_billing_setup_status',
	function( $status ) {
		return 'approved';
	}
);

Make a local development site publicly accessible

There are a few scenarios during local development when this setting is necessary for smooth operation:

  • Connecting to a WordPress.com account
  • Connecting to a Google Merchant Center account
  • Editing and saving campaign assets

Add the code snippet to wp-config.php:

// Code for ngrok
if ( ! empty( $_SERVER['HTTP_X_FORWARDED_HOST'] ) ) {
	$protocol = ( isset( $_SERVER['HTTP_X_FORWARDED_PROTO'] ) && 'https' === $_SERVER['HTTP_X_FORWARDED_PROTO'] ) ? 'https' : 'http';

	// Extra variables for ngrok forwarding https to http:88 if the dev site is localhost:88.
	if ( ! empty( $_SERVER['SERVER_NAME'] ) && str_ends_with( $_SERVER['SERVER_NAME'], ':88' ) ) {
		$_SERVER['REQUEST_SCHEME'] = $protocol;
		$_SERVER['HTTPS']          = 'https' === $protocol ? 'on' : 'off';
		$_SERVER['SERVER_NAME']    = $_SERVER['HTTP_X_FORWARDED_HOST'];
		$_SERVER['HTTP_HOST']      = $_SERVER['HTTP_X_FORWARDED_HOST'];
		$_SERVER['SERVER_PORT']    = $_SERVER['HTTP_X_FORWARDED_PORT'];
	}

	define( 'WP_HOME', $protocol . '://' . $_SERVER['HTTP_X_FORWARDED_HOST'] );
	define( 'WP_SITEURL', WP_HOME );
}

Using ngrok with a local site:

# Forwarding to http://localhost:80
ngrok http 80 --host-header=rewrite --url www.your-site.dev

# Or, forwarding to http://your-dev-site.local
ngrok http your-dev-site.local --host-header=rewrite --url www.your-site.dev

# Or, forwarding to https://your-dev-site.local
ngrok http https://your-dev-site.local --host-header=rewrite --url www.your-site.dev
Clone this wiki locally