Nicholas Cardot
Nicholas Cardot

Reputation: 964

How can I check if a page is loading via AMP in WordPress?

So in WordPress, there are a number of conditionals that allow you to run functions on certain parts of the site and not on others. For example:

is_feed() will allow you to target RSS feeds.
is_singular() will allow you to target single posts and pages.
is_amp() will allow you to target pages being served via the AMP version.

Just kidding on that last one. That function doesn't exist, but that's what I'm asking about. Is there a function or a mechanism that I can use to test if a page is being served via the AMP version of that page?

My desired code would be something like this:

if( true === is_singular() && !is_feed() && !is_amp() ):
    add_filter( 'the_content','swp_insert_pinterest_image', 10 );
endif;

Of course, is_amp() is too generic so I'll add my vendor prefixes and whatnot to avoid name collisions, but I digress.

So, how can I check for AMP in WordPress? Is there a function that we can write or perhaps there's a query parameter or $_GET variable that can be checked?

Upvotes: 1

Views: 4449

Answers (2)

Cory Webb
Cory Webb

Reputation: 46

To determine if you are currently on an AMP page, use is_amp_endpoint().

EDIT: is_amp_endpoint() is now deprecated. Use amp_is_request() as of v2.0.

Upvotes: 3

ThurstonLevi
ThurstonLevi

Reputation: 827

just to update on this, the above has been updated to amp_is_request()

Upvotes: 3

Related Questions