Site logo

[Resolved] Mobile Redirect for Front Page

Home Forums Support [Resolved] Mobile Redirect for Front Page

Home Forums Support Mobile Redirect for Front Page

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #1847124
    Ian

    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;
    }
    }

    #1847207
    Elvin
    Staff
    Customer Support

    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.

    #1848454
    Ian

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

    #1848461
    Elvin
    Staff
    Customer Support

    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.

    #1849289
    Ian

    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.

    #1849427
    Elvin
    Staff
    Customer Support

    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.

    #1851184
    Ian

    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!

    #1851396
    Ian

    It’s working now! Thanks Elvin!

    #1851397
    Ian

    REsolved

    #1852304
    Elvin
    Staff
    Customer Support

    No problem. Glad to be of any help. šŸ˜€

    #2205062
    melvin

    Dropping by to say thank you for the reliable guide.

    I used hook with js method

    Thanks Alvin

Viewing 11 posts - 1 through 11 (of 11 total)
  • You must be logged in to reply to this topic.