Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End single page mobile redirect:

  • This topic is empty.
Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #190643
    jayg-7
    Participant

    Hello, I’m using wordpress and trying to redirect users on a mobile device from a single page to another page. I’m only doing it for one page so I don’t require a full plugin.

    Here’s what I’m plugging into my functions.php:

    function welcome_mobile_redirect() {
        if (is_mobile() && get_the_ID() == 1114) {
        wp_redirect('http://necronomicon.studioissa.com/welcome/');
        }
    }

    But it’s not working yet. Any thoughts?

    #190654
    Alen
    Participant

    get_the_ID can only be used within a WordPress Loop. If you need to get the ID outside, check out this.

    But I would handle this differently by:

    • Create a page template page-{id}.php
    • Only check for mobile

    No need to check for current state from your functions on every load. Corresponding template will only load when that page loads so we off load that responsibility.

    #190655
    Alen
    Participant

    So your final code would be

    
    $url = 'http://necronomicon.studioissa.com/welcome/';
    if ( is_mobile() ) wp_redirect( $url, 301 ); exit;
    
    #191251
    jayg-7
    Participant

    Hi Alen,

    Thanks so much. I’m already using a custom template for that page. But when I put this code in it doesn’t work.

    Does this code need to be anywhere specific?

    Thanks!

    #191254
    Alen
    Participant

    @jayg-7

    Sorry that should be wp_is_mobile not is_mobile.

    so,

    $url = 'http://necronomicon.studioissa.com/welcome/';
    if ( wp_is_mobile() ) wp_redirect( $url, 301 ); exit;
    
    #251352
    erjdavis
    Participant

    Here is a plugin that would allow you to accomplish this – http://www.ultimate-wordpress-mobile-redirect.com

    The plugin allows you to set a unique redirect URL for every page and post on your wordpress website. It works for mobile and tablets.

    Hope that helps!

Viewing 6 posts - 1 through 6 (of 6 total)
  • The forum ‘Back End’ is closed to new topics and replies.