WordPress AMP Plugin Errors due to New Relic Javascript Injection

I was working on a high profile website under a big name and they had me implement the AMP plugin a couple months ago.

Pages were working great on mobile thanks to the WordPress AMP Plugin. Then after three months that’s when a bunch of errors started showing up on the Google AMP console and I later learned that these errors were very common.

But there was one particular error that got me stumped for a while. It was the “User Authored Javascript Found on Page” error. AMP doesn’t like foreign looking Javascript like that, so it was acting up and spitting out an error – that’s all that was.

I was curious about what piece of javascript was causing that though. I went for a journey through the source code and found that the javascript was injected by the New Relic API. This particular site does make use of a spectrum of technologies and infrastructure – so it totally made sense. But back to the task at hand – let’s get that javascript out and move on with our lives.

You want to put the below snippet of code in your functions.php theme file.

//Remove new relic from amp pages
if (function_exists('newrelic_disable_autorum')) {
    add_action('init', function() {
        $url = $_SERVER['REQUEST_URI'];
        if(strpos($url, '/amp/') !== false){   
            newrelic_disable_autorum();
        }
    });
};

The important part here actually is first checking to see if the new relic function exists then run the following action. Otherwise you will run into additional errors if let’s say the New Relic was removed entirely from the site – this would still run and you would be shooting darts in the dark again.

Basically were checking to see ‘/amp/’ is detected in a URL – if so, then disable the javascript injected by New Relic.