Search code examples
wordpresswordpress-theming

Wordpress redirect when viewed by desktop or mobile


I have done quite a bit of googling and it's possible my wording is not providing the correct results.

  1. I have a child theme set up
  2. I want to add 2 redirects
  3. Redirect 1: if user is viewing site from desktop redirect to new website.
  4. Redirect 2: I am adding the website via a webview in a mobile app and if site is not viewed from package name redirect to new website.

Issues I have faced are when I go to the wp-admin to login I get redirected to new url since i am technically viewing the website with desktop browser. so I need to exclude all the admin/dashboard/all the pages that one uses to build a wordpress site.

Also have received this "Warning: Cannot modify header information - headers already sent by" quite a bit. I would share what I have tried but I have changed code so much that I deleted it all so my site would work again for now.

Thank you in advance for any assistance.


Solution

  • It depends on how you're handling the redirect, but:

    //  Redirect condition
    
    if (!is_admin()) {
        if (!wp_is_mobile()) {
            // If not using mobile
            wp_redirect('https://your_desktop_site.com');
            exit;
        }
    }
    

    With regards to the errors you're seeing: again, this is going to be down to how you're handling the redirect. However you're assessing the condition to determine what platform they're browsing on, you should probably hook in pretty early, for example on init, template_redirect or wp.