How to redirect websites in mobile with m.domain.com in wordpress

Hello readers,
Here i have come with some solution for the wordpress site, how you redirect your website www.domain.com to m.domain.com.

If you want to redirect your site in mobile with m.domain.com then you have to add following code in your theme functions.php file

function redirect_mobile(){
   if ( wp_is_mobile() ) {
    wp_redirect( 'http://url' ); 
    exit; 
  }
}

add_action('wp_head', 'redirect_mobile'); // Action that check agent from broser.

In this wp_is_mobile() this will checks if the user is visiting using a mobile device. This is a boolean function, meaning it returns either TRUE or FALSE. It works through the detection of the browser user agent string ($_SERVER['HTTP_USER_AGENT'])

Leave a Reply

Your email address will not be published. Required fields are marked *