Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[OBSOLETE] Proof of concept extension to the official AMP plugin for redirecting mobile visitors to the AMP version when it is available. Consider using with AMP-to-AMP Linking: https://gist.github.com/westonruter/f9ee9ea717d52471bae092879e3d52b0
<?php
/**
* AMP Mobile Redirection plugin file.
*
* @package AMP_Mobile_Redirection
* @author Weston Ruter, Google
* @link https://gist.github.com/westonruter/787c875e5ab724808c26014153e46ec6
* @license GPL-2.0-or-later
* @copyright 2019 Google Inc.
*
* @wordpress-plugin
* Plugin Name: AMP Mobile Redirection
* Plugin URI: https://gist.github.com/westonruter/787c875e5ab724808c26014153e46ec6
* Description: Proof of concept for redirecting mobile visitors to the AMP version when it is available. Consider using with the <a href="https://gist.github.com/westonruter/f9ee9ea717d52471bae092879e3d52b0/edit">AMP-to-AMP Linking</a> plugin.
* Author Name: Weston Ruter
* Author URI: https://weston.ruter.net/
* Version: 0.2
* License: GPLv2 or later
* Gist Plugin URI: https://gist.github.com/westonruter/787c875e5ab724808c26014153e46ec6
*/
add_action(
'template_redirect',
function () {
// Make sure AMP plugin is available.
if ( ! function_exists( 'is_amp_endpoint' ) || ! method_exists( 'AMP_Theme_Support', 'is_paired_available' ) ) {
return;
}
/*
* Do not redirect if theme support not present. In other words, redirection only applies in Native/Transitional mode.
* When in Reader mode, redirection needs to be prevented to ensure the user can exit Reader mode.
*/
if ( ! current_theme_supports( 'amp' ) ) {
return;
}
// @todo Is this needed? Do search bots still need to access the non-AMP version? Should this be fleshed out more to prevent mobile search bots from being able to access the non-AMP version?
$is_bot = ( isset( $_SERVER['HTTP_USER_AGENT'] ) && preg_match( '/Googlebot|Bingbot|validator\.ampproject\.org/', $_SERVER['HTTP_USER_AGENT'] ) );
// ⚠️ Warning: Reverse proxy caches must be configured to vary the cache by the user agent.
$should_redirect = (
! is_amp_endpoint()
&&
wp_is_mobile()
&&
! $is_bot
&&
AMP_Theme_Support::is_paired_available()
);
if ( $should_redirect ) {
$new_url = add_query_arg( amp_get_slug(), '', amp_get_current_url() );
wp_safe_redirect( $new_url, 302 );
exit;
}
}
);
@maweb71

This comment has been minimized.

Copy link

@maweb71 maweb71 commented May 2, 2019

Hi, i've installed amp-mobile-redirection and amp-to-amp plugin too.

With normal phone (5 inches, android 6.0), it works fine, redirect to /?amp post correctly.

Instead with a tablet (samsung galaxy tab 3, 8 inches, android 4.4.4), it not redirect to /?amp but shows the normal one.

I use Astra wp theme paired only.

@Acheetahpurr

This comment has been minimized.

Copy link

@Acheetahpurr Acheetahpurr commented Dec 6, 2019

Hi,

I just have installed this plugin, but there is a conflict between Newspaper theme and this plugin, I mean my AMP Menu doesn't display as normal with arrows on the right to group subcategories and articles.

Because without “AMP Mobile Redirection” sometimes tablets visitors are redirected to dekstop and sometimes AMP. So I need AMP for All tablets visitors, or Desktop for all tablets visitors ?

Is there any other code, or plugin I can enter/install to be sure tablets visitors are only redirected to AMP, or only to desktop ? I mean something in link with Breakpoint or I don’t know.

Thank you

https://typemeon.com/?amp

@westonruter

This comment has been minimized.

Copy link
Owner Author

@westonruter westonruter commented Dec 6, 2019

@Acheetahpurr I'm not sure. If you want to include support for directing tablet visitors to AMP, then you may need to replace wp_is_mobile() with something that is more specific to tablets. The Jetpack plugin includes a jetpack_is_mobile() function which is more robust for this purpose.

Otherwise, what you may be seeing is issues with caching.

In all, I suggest contacting tagDiv support regarding this.

@Acheetahpurr

This comment has been minimized.

Copy link

@Acheetahpurr Acheetahpurr commented Dec 6, 2019

Hi,

thank you for you reply, I'm a beginner, I am using WP fastest cache premium, and when "Mobile Theme - Create cache for mobile theme "option is on, sometimes tablets visitors are redirected to desktop, and sometimes AMP, it depends. So that's why I was looking for a way to redirect them only on AMP or Desktop. So this morning I deactivated "Mobile Theme - Create cache for mobile theme " but I activated "Mobile - Don't show the cached version for desktop to mobile devices". Everything works now I think, even my amp menu, but I have no cache for mobile theme, no ? or Maybe there is already a mobile cache with amp plugin ? Sorry, maybe I make you confused, but my mobile theme "newspaper" is running with AMP plugin.

Kind regards

@westonruter

This comment has been minimized.

Copy link
Owner Author

@westonruter westonruter commented Dec 6, 2019

I suggest contacting the WP Fastest Cache support.

@westonruter

This comment has been minimized.

Copy link
Owner Author

@westonruter westonruter commented Feb 1, 2020

@Acheetahpurr Here's a new approach on redirecting mobile visitors to the AMP version: ampproject/amp-wp#2044 (comment)

@westonruter

This comment has been minimized.

Copy link
Owner Author

@westonruter westonruter commented Apr 4, 2020

@westonruter

This comment has been minimized.

Copy link
Owner Author

@westonruter westonruter commented Sep 4, 2020

The functionality in this plugin is now part of the AMP plugin as of v2.0. Please deactivate this plugin and upgrade the AMP plugin.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment