From 2cab5bbf83f62c6f14725de7f50b6eb6c4781116 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Mon, 5 Feb 2018 10:24:09 -0800 Subject: [PATCH] Prevent New Relic script from being injected in AMP responses --- amp.php | 5 +++++ includes/class-amp-theme-support.php | 11 +++++++++++ 2 files changed, 16 insertions(+) diff --git a/amp.php b/amp.php index 1b63a1e7310..b800539c6f5 100644 --- a/amp.php +++ b/amp.php @@ -316,6 +316,11 @@ function amp_render_post( $post ) { $wp_query->query_vars[ AMP_QUERY_VAR ] = true; } + // Prevent New Relic from causing invalid AMP responses due the NREUM script it injects after the meta charset. + if ( extension_loaded( 'newrelic' ) ) { + newrelic_disable_autorum(); + } + /** * Fires before rendering a post in AMP. * diff --git a/includes/class-amp-theme-support.php b/includes/class-amp-theme-support.php index 01983ef66fa..ffae0b69ea0 100644 --- a/includes/class-amp-theme-support.php +++ b/includes/class-amp-theme-support.php @@ -796,6 +796,17 @@ protected static function ensure_required_markup( DOMDocument $dom ) { * @see AMP_Theme_Support::finish_output_buffering() */ public static function start_output_buffering() { + /* + * Disable the New Relic Browser agent on AMP responses. + * This prevents th New Relic from causing invalid AMP responses due the NREUM script it injects after the meta charset: + * https://docs.newrelic.com/docs/browser/new-relic-browser/troubleshooting/google-amp-validator-fails-due-3rd-party-script + * Sites with New Relic will need to specially configure New Relic for AMP: + * https://docs.newrelic.com/docs/browser/new-relic-browser/installation/monitor-amp-pages-new-relic-browser + */ + if ( extension_loaded( 'newrelic' ) ) { + newrelic_disable_autorum(); + } + ob_start(); // Note that the following must be at 0 because wp_ob_end_flush_all() runs at shutdown:1.