Overrides::add_amp_redirect_headers( string $url )

Summary

AMP Add Redirect Headers


Description

Identify if we are attempting a redirect from an AMP form and if so, add the proper headers to redirect in AMP.


Parameters

$url

(string) (Required) URL to be redirected to.


Source

File: src/BigCommerce/Amp/Overrides.php

	public function add_amp_redirect_headers( $url ) {
		$amp_source_origin = filter_input( INPUT_GET, '__amp_source_origin', FILTER_SANITIZE_STRING ) ?: false;

		if ( $amp_source_origin ) {
			// Ensure we have an absolute path for the redirect URL.
			$parsed_url = wp_parse_url( $url );

			if ( ! isset( $parsed_url['scheme'] ) && ! isset( $parsed_url['host'] ) ) {
				if ( '/' === substr( $url, 0, 1 ) ) {
					$url = untrailingslashit( get_site_url() ) . $url;
				} else {
					$url = trailingslashit( get_site_url() ) . $url;
				}
			}

			$url = add_query_arg( 'amp', true, $url );

			// Add the AMP headers so the redirect works properly and doesn't throw a console error.
			header( 'AMP-Redirect-To: ' . esc_url_raw( $url ) );
			header( 'AMP-Access-Control-Allow-Source-Origin: ' . esc_url_raw( $amp_source_origin ) );
			header( 'Access-Control-Expose-Headers: AMP-Access-Control-Allow-Source-Origin, AMP-Redirect-To' );
			wp_send_json_success();
		}
	}

User Contributed Notes

You must log in before being able to contribute a note or feedback.