• Hi, I need a little hand on how to achieve this if possible please..
    I have had a quick Google but no real avail…

    I need to redirect most (but not all) pages if a user is logged out, now I have found how to redirect all my pages if user is logged out but I need logged out users to still have access to some pages, those being:
    >> My custom “Registration” Page
    >> My “ToS & PP” page
    >> My “Contact Us” page

    Is this possible?
    If so, how do I go about doing it?

    P.S.
    If it helps at all, THIS IS MY SITE

Viewing 11 replies - 1 through 11 (of 11 total)
  • I would do something like

    $restricted_pages = array(1,2,3);
    
    if (in_array(get_the_ID(),$restricted_pages))
      if (!is_user_logged_in())
        wp_redirect('https://..');

    Where $restricted_pages are the ID’s of the logged-in-only pages, and https://.. is the location of the redirect.

    the codes off the top of my head, not tested.

    oh and you might want to throw it in init hook

    add_action('init','boot_non_users');
    function boot_non_users() {
      ...
    }
    Thread Starter sicky_brazz

    (@sicky_brazz)

    Thanks for the reply @davidsword

    Unfortunately the code doesn’t seem to work ??
    This is probably me putting it the wrong file or not fully understanding what was needed as I’m not too clued up on coding.

    Anyway, I placed the following code into my child themes function.php to test on two pages (2,29) but as above, no luck..

    add_action(‘init’,’boot_non_users’);
    function boot_non_users() {
    $restricted_pages = array(2,29);

    if (in_array(get_the_ID(),$restricted_pages))
    if (!is_user_logged_in())
    wp_redirect(‘https://www.carfreaksunite.com/car-freaks-unite’);
    }

    P.S.
    Is there a way of doing is so that ALL pages are redirected and then add an exclusion list?
    It’s just I have shed a loads of pages, sub pages etc. that I would have to go through to get the page ID’s and only 3 that would need the exclusion…

    my mistake, try changing 'init' to 'wp'

    Thread Starter sicky_brazz

    (@sicky_brazz)

    @davidsword

    Working a treat now ??

    Thanks a lot, really appreciated.

    P.S.
    I edited last post and added a P.S. to it, if you could update on that, things would be awesome, cheers.

    it’d be something like

    add_action('init','boot_non_users');
    function boot_non_users() {
      $world_pages = array(1,2,3);
    
      if (!in_array(get_the_ID(),$world_pages))
         if (!is_user_logged_in())
            wp_redirect('https://www.carfreaksunite.com/car-freaks-unite');
    }

    and to get the code to look like code, use back ticks. or the “CODE” formatting button above where you type.

    Thread Starter sicky_brazz

    (@sicky_brazz)

    Thanks again @davidsword

    Unfortunately the latest code doesn’t seem to be working, I have tried changing “init” to “wp” as I did the other code but still the exact same issue..

    I get the following results:

    Firefox has detected that the server is redirecting the request for this address in a way that will never complete.

    ^^similar results in other browsers as well^^

    You have been a great help so far, very much appreciated.

    Thread Starter sicky_brazz

    (@sicky_brazz)

    @davidsword

    I have gone back to the working code for now (3rd post down) and still only using the page Id’s 2,29 in the code for the time being as I would still prefer to use the other code you provided if you would kindly amend it to work.

    Also, I did fail to mention last time that the “working code” still allows logged out users access on page ID 29 (my bbpress forums), is there a reason why?

    Sorry to keep pestering you but I do kind of need this sorted, and again, thanks for your help so far.

    Please advise.
    Cheers.

    the firefox notice is because you’re page you’re directing people to for the redirect isn’t on the $world_pages “safe” list. so it’s an infinite loop.

    everything I’ve provided should be more than enough answer.

    it’s probably not working on bbforums as get_the_ID() == 29 must not be true for the entire bbforums forums, (which makes sense). google something like “how to tell if on bbforums page” to find a function that you can argue and insert that into the code.

    I think you may be looking for is_bbpress() which should do what you are after, there are also a few more conditionals on the following page and quite a bit of reference code scattered throught the bbPress forums here.

    https://codex.bbpress.org/bbpress-conditional-tags/

    Thread Starter sicky_brazz

    (@sicky_brazz)

    @davidsword
    I have had a Google about $world_pages and “safe” list but to no relevance at all, please can you explain or tell me how to get my site onto the “safe” list, or even what this “safe” list is.
    Thanks.

    @stephen-edgar
    I am no coder by any means and all of this is going straight over my head, any chance you could explain what I need to know in idiots terms please.
    Thanks.

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘How can I redirect select pages when user is logged out?’ is closed to new replies.