Tag Archives: amp

Redirecting to AMP (reader mode) for mobile

I’m not a big fan of AMP but I do have it active here on this blog using the official AMP plugin for WordPress, using it in “Reader” (aka “classic”) mode. That’s as far as I want to take it, but suppose (as was the case for an Autoptimize/ Critical CSS user I was helping out) you want to redirect all mobile traffic to AMP, then you could use below code snippet to do just that.

  add_action('init','amp_if_mobile');  function amp_if_mobile() {    $_request = $_SERVER['REQUEST_URI'];  	if ( strpos( $_request, '?amp' ) === false && strpos( $_request, '&amp' ) === false && ! is_admin() && wp_is_mobile() ) {      if ( strpos( $_request, '?' ) === false ) {        $_amp = '?amp';      } else {        $_amp = '&amp';      }      $amp_url = home_url() . $_request . $_amp;      wp_redirect( $amp_url );      exit;    }  }  

It might be a little rough around the edges, but it (mostly) gets the job done ;-)