Last active
April 21, 2017 19:46
-
-
Save surajair/216e57198313227d46afca5b81f04318 to your computer and use it in GitHub Desktop.
WP mobile redirect for AMP
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 | |
function wp_ampify_template_redirect(){ | |
if (!wp_is_mobile() ) | |
return; | |
$cur_url = esc_url((is_ssl() ? "https://" : "http://") . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"] ); | |
if(is_front_page() || is_home() || is_archive()){ | |
$cur_url .= '?amp=1'; | |
} | |
else{ | |
$cur_url .= 'amp'; | |
} | |
// make sure we don't redirect to ourself | |
if (function_exists('is_amp_endpoint') && !is_amp_endpoint()) { | |
header("Cache-Control: max-age=0, no-cache, no-store, must-revalidate"); //from amclin | |
wp_redirect($cur_url); | |
exit; | |
} | |
} | |
add_action('template_redirect', 'wp_ampify_template_redirect'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment