How to check if the current page is an AMP page
Use amp_is_request() from the AMP plugin to branch your theme code safely: a guarded helper, a different sidebar for AMP and common timing mistakes.
At a glance
If your site uses the official AMP plugin, amp_is_request() tells you whether the current request is AMP. Wrap it in a function that checks the plugin exists, and call it only after the main query has run, for example in template files or on the wp action. This guide shows a guarded helper, an AMP-only sidebar and the common timing mistakes.
Updated November 29, 2026 · For WordPress theme developers using the official AMP plugin
- For theme developers branching markup for AMP and non-AMP requests
- Function amp_is_request(), which replaces the deprecated is_amp_endpoint()
- Guard function_exists() so deactivating the plugin does not cause a fatal error
- Timing only after the query is parsed; not on init or plugins_loaded
- Modes standard, transitional and reader behave differently
- Safety back up and use a child theme or small plugin
- Help theme development and WordPress development services
On this page
If your site uses the official AMP plugin, you sometimes need different markup for AMP and non-AMP requests, such as a simpler sidebar, no custom JavaScript or an AMP-safe ad. The plugin gives you `amp_is_request()`. Wrap it in a function that checks the plugin exists, and call it only after the main query has run.
What is amp_is_request() and why guard it?
The AMP plugin provides amp_is_request(). It returns true when the current request is served as AMP, in any mode (standard, transitional or reader), and false otherwise. It replaced the deprecated is_amp_endpoint().
If the plugin is deactivated, a direct call ends in a fatal error: "Call to undefined function". Wrap it:
function mysite_is_amp(): bool {
return function_exists( 'amp_is_request' ) && amp_is_request();
}Use your own prefix instead of mysite_ so the name cannot clash. The return type needs PHP 7 or later, which any supported WordPress already requires.
When can I call it?
| Safe | Not safe |
|---|---|
Template files such as header.php, sidebar.php, single.php | plugins_loaded |
Callbacks on wp, template_redirect, wp_head, wp_enqueue_scripts and later | after_setup_theme |
init |
Calling it too early makes the AMP plugin log a "doing it wrong" notice, and the result may be unreliable. For example, to skip a script on AMP pages:
add_action( 'wp_enqueue_scripts', function () {
if ( mysite_is_amp() ) {
return;
}
wp_enqueue_script( 'mysite-extras', get_stylesheet_directory_uri() . '/js/extras.js', array(), '1.0.0', true );
} );Custom JavaScript is generally not allowed in AMP pages, so this keeps the page valid.
What are the three AMP modes?
| Mode | How it works | Effect on amp_is_request() |
|---|---|---|
| Standard (AMP-first) | Every page is AMP, one version of the site | True on all front-end pages |
| Transitional | Each page has a normal and an AMP version at related URLs | True only on the AMP one |
| Reader | AMP versions use a separate, simplified template | Theme code mostly does not run there |
Check the mode in the AMP plugin's settings before you write anything.
How do I vary the sidebar and template parts?
When a section such as a share bar needs a different file for AMP, use the second argument of get_template_part():
get_template_part( 'template-parts/share', mysite_is_amp() ? 'amp' : 'default' );This loads share-amp.php or share-default.php. If a named file does not exist, WordPress falls back to share.php, so keep one of them.
- Add a body class:
add_filter( 'body_class', fn( $c ) => mysite_is_amp() ? array_merge( $c, array( 'is-amp' ) ) : $c );. - Skip ads or embeds that break AMP validity, or print an AMP-compatible alternative.
- Print analytics differently, since AMP uses its own component.
How do I test it?
- Open a normal page and its AMP version
In standard mode every page is AMP; in transitional or reader mode add
?amp=1or use/amp/. - Check the markup
has the expected sidebar.
- Check validity
under AMP, Validated URLs or the admin bar item.
- Check the normal page
is untouched.
- Deactivate the AMP plugin
and confirm the site works and the helper returns
false.
What mistakes are common?
- Calling
amp_is_request()oninitorplugins_loadedand getting a notice. - Calling it without the
function_exists()guard, then deactivating the plugin. - Using the deprecated
is_amp_endpoint(). - Adding JavaScript to AMP pages, which fails validation.
- Testing only the normal page, and finding AMP errors in Search Console weeks later.
- Branching in the theme when a plugin, not the theme, loads the offending script.
Do I need AMP at all, and what about caching?
AMP once gave a layout benefit in Google's mobile news carousel. In 2021 Google removed the AMP requirement for Top Stories, and page experience applies to any page. If normal pages already pass Core Web Vitals, a second version may cost more than it returns, so compare AMP and non-AMP traffic first. If you drop it, remove the plugin, redirect AMP URLs to the normal ones and clean up the branching code.
If a host or plugin caches pages, cache AMP and non-AMP separately. Page caches usually key on the URL, which is enough in transitional mode, but can go wrong if the same URL serves different markup. Clear the cache after deploying branching code and check both versions in a private window.
Our developers can fix validation or retire AMP without losing traffic.
For help see our theme developers and WordPress development team. For performance work that does not depend on AMP, see fixing native lazy loading.
What function tells me if the current request is AMP?
amp_is_request(), provided by the official AMP plugin. Call it only after WordPress has parsed the query, for example inside template files or on the wp action.
Link to this answerWhy does amp_is_request() trigger a notice?
Calling it too early, such as at plugin load or on init, happens before WordPress knows which page is being requested, so the answer would be unreliable. Move the check later.
Link to this answerDo I need to change my caching setup for AMP?
Make sure AMP and non-AMP versions are cached separately. In transitional or reader mode the URL differs, which usually keeps them apart, but clear caches after deploying code that branches on AMP.
Link to this answerDo I still need AMP for Google rankings?
No. Google dropped AMP as a requirement for features like Top Stories in 2021. Keep AMP only if it still serves your readers or a partner.
Link to this answerWhy guard the call with function_exists()?
If the AMP plugin is deactivated or removed, a direct call ends in a fatal error: Call to undefined function. A guarded helper lets your theme survive without the plugin.
Link to this answerWhat happened to is_amp_endpoint()?
It is deprecated and replaced by amp_is_request(). Avoid using it in new code.
Link to this answerWhat are the three AMP modes?
Standard (AMP-first) serves every page as AMP, transitional gives each page a normal and an AMP version, and reader uses a separate simplified template. The mode changes what your branching code affects.
Link to this answerHow do I swap one template part for AMP?
Use the second argument of get_template_part(), for example mysite_is_amp() ? 'amp' : 'default'. WordPress falls back to the base file if a named one is missing.
Link to this answerHow do I test AMP branching?
Open a normal page and its AMP version (?amp=1 or /amp/ in transitional or reader mode). Check the markup, the validity status under AMP, Validated URLs, that the normal page is untouched, and that the site works with the plugin deactivated.
Link to this answerCan I add JavaScript to AMP pages?
Custom JavaScript is generally not allowed in AMP pages. Skip enqueueing your script when the request is AMP to keep the page valid.
Link to this answerKeep reading
Want an engineer on it?
Tell us what is wrong in plain words. A person reads it, today.