2

I have a responsive website which is accessed via login only. All pages can be accessed via mobile / desktop it's just we thought having a different page for mobile users was more appropriate.

I previously asked to redirect mobile users to a different landing page and got this answer (which works very well):

add_filter('login_redirect', function($to, $request, $user)
{
    if ( wp_is_mobile() ) $to = home_url().'/mobile-news';

    return $to;
}, PHP_INT_MAX, 3);

Currently if a user is linked to a news article (e.g. site.com/exciting-news-article) they will be met with the login page (this is normal) however once they log in the redirect request is ignored in favour of (site.com/mobile-news) page.

I need to adapt this so that if they're linked to a specific news item they'll go there instead of the mobile-news page.

I've been trying different solutions but it just seems to ignore completely. This is what I currently have and it's not working correctly:

add_filter('login_redirect', function($to, $request, $user)
{
    if ( wp_is_mobile() ) {
      if ($_GET['redirect_to'] != '') {
          $to = $_GET['redirect_to'];

      }
      else {
          $to =  home_url().'/mobile-news';
      }
    }
    return $to;
}, PHP_INT_MAX, 3);

Has anyone come across this problem before - I'm not sure where I'm going wrong!

1

Try this one-

add_filter('login_redirect', function($to, $request, $user)
{
    if ( wp_is_mobile() && $request == null ) {
        return home_url().'/mobile-news';
    }
    return $to;
}, PHP_INT_MAX, 3);

Not tested, but should work.

Note: using a URL like this one home_url().'/mobile-news' is not a good idea, I think.

Use get_permalink( $post_id ) instead. Where $post_id is the ID of home_url().'/mobile-news' page. Because, if the slug of that page somehow got changed, visitors will see a 404 page!

| improve this answer | |
  • You are a star - thank you! As suggested I changed it to get_permalink. Thanks for your help – LucyMo Aug 16 '16 at 15:23
  • 1
    Never use wp_is_mobile(). It is broken at best and will not work with any cache. – fuxia Aug 16 '16 at 15:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.