Skip to content
Development

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.

By WP24×7 engineers4 min readPublished June 26, 2021Updated November 29, 2026

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

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?

SafeNot safe
Template files such as header.php, sidebar.php, single.phpplugins_loaded
Callbacks on wp, template_redirect, wp_head, wp_enqueue_scripts and laterafter_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?

ModeHow it worksEffect on amp_is_request()
Standard (AMP-first)Every page is AMP, one version of the siteTrue on all front-end pages
TransitionalEach page has a normal and an AMP version at related URLsTrue only on the AMP one
ReaderAMP versions use a separate, simplified templateTheme 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?

Register a second sidebar so the AMP version only holds AMP-compatible widgets:

add_action( 'widgets_init', function () {
	register_sidebar( array(
		'name'          => esc_html__( 'AMP Sidebar', 'mysite' ),
		'id'            => 'amp-sidebar',
		'description'   => esc_html__( 'Widgets shown on AMP pages only.', 'mysite' ),
		'before_widget' => '<section id="%1$s" class="widget %2$s">',
		'after_widget'  => '</section>',
		'before_title'  => '<h2 class="widget-title">',
		'after_title'   => '</h2>',
	) );
} );

Then in sidebar.php:

<?php
$sidebar_id = mysite_is_amp() ? 'amp-sidebar' : 'sidebar-1';

if ( is_active_sidebar( $sidebar_id ) ) : ?>
	<aside class="sidebar">
		<?php dynamic_sidebar( $sidebar_id ); ?>
	</aside>
<?php endif; ?>

Replace sidebar-1 with your theme's main sidebar ID, then add widgets under Appearance, Widgets. Block themes have no sidebar.php, so use template parts and conditional rendering instead.

How do I test it?

  1. Open a normal page and its AMP version

    In standard mode every page is AMP; in transitional or reader mode add ?amp=1 or use /amp/.

  2. Check the markup

    has the expected sidebar.

  3. Check validity

    under AMP, Validated URLs or the admin bar item.

  4. Check the normal page

    is untouched.

  5. Deactivate the AMP plugin

    and confirm the site works and the helper returns false.

What mistakes are common?

  • Calling amp_is_request() on init or plugins_loaded and 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.

AMP errors or retiring AMP?

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.

FAQ

Questions people ask

11 answers. Can't find yours? Ask an engineer.

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 answer
Why 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 answer
Do 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 answer
Do 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 answer
Why 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 answer
What happened to is_amp_endpoint()?

It is deprecated and replaced by amp_is_request(). Avoid using it in new code.

Link to this answer
What 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 answer
How do I use a different sidebar on AMP pages?

Register a second sidebar with register_sidebar(), then in sidebar.php pick its ID with a ternary on your guarded helper and print it only if is_active_sidebar() is true. Block themes have no sidebar.php, so use template parts instead.

Link to this answer
How 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 answer
How 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 answer
Can 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 answer

Keep reading

Want an engineer on it?

Tell us what is wrong in plain words. A person reads it, today.