Last active
December 7, 2020 19:44
-
-
Save milindmore22/27155b9dd5ca8ed9f2cfce545c1b89d7 to your computer and use it in GitHub Desktop.
AMP server side redirect
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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' ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Server-side redirection with WP Super cache.