Skip to content

Instantly share code, notes, and snippets.

@surajair
Last active April 21, 2017 19:46
Show Gist options
  • Save surajair/216e57198313227d46afca5b81f04318 to your computer and use it in GitHub Desktop.
Save surajair/216e57198313227d46afca5b81f04318 to your computer and use it in GitHub Desktop.
WP mobile redirect for AMP
<?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