Skip to content

Instantly share code, notes, and snippets.

@milindmore22
Last active December 7, 2020 19:44
Show Gist options
  • Save milindmore22/27155b9dd5ca8ed9f2cfce545c1b89d7 to your computer and use it in GitHub Desktop.
Save milindmore22/27155b9dd5ca8ed9f2cfce545c1b89d7 to your computer and use it in GitHub Desktop.
AMP server side redirect
<?php
add_action(
'wp',
function() {
add_action( 'amp_mobile_client_side_redirection', '__return_false' );
if ( ! isset( $_GET['noamp'] ) && wp_is_mobile() && function_exists( 'is_amp_endpoint' ) && ! is_amp_endpoint() && function_exists( 'amp_is_available' ) && amp_is_available() ) {
wp_safe_redirect( esc_url( add_query_arg( 'amp', '1', get_permalink() ) ), 302 );
}
}
);
if ( function_exists( 'add_cacheaction' ) ) {
if ( ! isset( $_GET['noamp'] ) && function_exists( 'is_amp_endpoint' ) && ! is_amp_endpoint() && function_exists( 'amp_is_available' ) && amp_is_available() ) {
add_cacheaction( 'wp_cache_check_mobile', 'amp_check_mobile' );
}
}
/**
* AMP check cache type.
*/
function amp_check_mobile() {
if ( wp_is_mobile() ) {
add_filter( 'amp_pre_is_mobile', '__return_true' );
}
}
@milindmore22
Copy link
Author

Server-side redirection with WP Super cache.

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