<?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; | |
} | |
} | |
); |
This comment has been minimized.
This comment has been minimized.
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 |
This comment has been minimized.
This comment has been minimized.
@Acheetahpurr I'm not sure. If you want to include support for directing tablet visitors to AMP, then you may need to replace Otherwise, what you may be seeing is issues with caching. In all, I suggest contacting tagDiv support regarding this. |
This comment has been minimized.
This comment has been minimized.
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 |
This comment has been minimized.
This comment has been minimized.
I suggest contacting the WP Fastest Cache support. |
This comment has been minimized.
This comment has been minimized.
@Acheetahpurr Here's a new approach on redirecting mobile visitors to the AMP version: ampproject/amp-wp#2044 (comment) |
This comment has been minimized.
This comment has been minimized.
Installation instructions: https://gist.github.com/westonruter/6110fbc4bef0c4b8c021a112012f7e9c |
This comment has been minimized.
This comment has been minimized.
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. |
This comment has been minimized.
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.