Site logo

Archived topic

Mobile Redirect for Front Page

10 replies · Started by Ian on July 6, 2021

Viewing posts 1–11 of 11

Hello, I've followed another thread by Tom and added this snipped to my website thelitunderground.com

However, it is no redirecting on mobile. Am I missing a step? Thanks!

add_action( 'wp', 'tu_redirect_mobile' );
function tu_redirect_mobile() {
if ( wp_is_mobile() AND is_front_page() ) {
wp_redirect( 'https:/thelitunderground.com/podcasts', 301 );
exit;
}
}

Hi Ian,

can you try this instead?

add_action( 'wp', 'tu_redirect_mobile' );
function tu_redirect_mobile() {
    if ( wp_is_mobile() && is_front_page() ) {
        wp_redirect( 'https://thelitunderground.com/podcasts', 301 );
        exit;
    }
}

Let us know how it goes.

Hey Elvin, that one didn't end up working.

It's working on my end.

You can visit this page - https://dev-generate-press.pantheonsite.io/

When I visit this page on my Android phone (chrome), I get redirected to your site's podcasts page.

It should be the same when you try to visit it from your phone's browser.

Note: I don't think this kind of redirection is reliable though. Even the official documentation doesn't recommend this - https://developer.wordpress.org/reference/functions/wp_is_mobile/ - WordPress core's wp_is_mobile() is not reliable. It's better to rely on JS for this.

Consider trying this:

Create a Hook element. Set the display rule location to "Front page" and then add this code.

<script>
if( /Android|webOS|iPhone|iPad|Mac|Macintosh|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
	location.href = "https://thelitunderground.com/podcasts";
}</script>

This should redirect the visitor to the href specified. If the page visited on a detected mobile device specified on the condition.

Thanks for the help Elvin! I just added a hook, set a rule to the front page and added the code. Is it working on your end? It isn't working on my phone.

Thanks for the help Elvin! I just added a hook, set a rule to the front page and added the code. Is it working on your end? It isn’t working on my phone.

It's working on my end when I visit your site. On my phone (Android, Chrome), when I try to visit the frontpage, it brings me to the /podcasts/ page immediately.

Hey Elvin, so I see that its working for Android on Chrome. However, it is not working for Safari on Iphone on the phones around my house. Thanks!

It's working now! Thanks Elvin!

REsolved

No problem. Glad to be of any help. :D

Dropping by to say thank you for the reliable guide.

I used hook with js method

Thanks Alvin

This archived topic is closed to new replies.