• I have two sets of navbar menu’s, one for logged in users one for non logged in users, and at the moment if they know the url of the pages in the 2nd logged in menu pages, then they can access them.

    I am using the code belwo to stop access to them unless they are logged in and its not having any effect at all, but it seems ok to me.

    add_action( 'template_redirect', function() {
    
      if ( !is_user_logged_in() && ! is_page() ) return;
    
      $restricted = array( 'sim-racing', 'support-forum' ); // all restricted pages
    
      if ( in_array( get_queried_object_id(), $restricted ) ) {
        wp_redirect( 'https://www.mywebsite.com/support-forum/', 301 ); 
        exit();
      }
    });
Viewing 2 replies - 1 through 2 (of 2 total)
  • i used ‘allow PHP in post’ plugin and able to hide content if the user is not logged in. (also, i can display content based on user’s access permission.
    i simply use user’s last name and assign ‘GROUP’ to determine what content will be shown.
    (ie GroupA, GroupB etc). each group will see different content.
    sample is below
    ———– start code —————-
    [php]
    global $current_user;
    get_currentuserinfo();
    $pos5 = strrpos($current_user->user_lastname, ‘GroupB’);

    if ( is_user_logged_in() )
    {
    if ($current_user->user_login == ‘admin’ || $current_user->user_lastname == ‘Editor’ || !($pos5===false) ) {
    echo (‘<hr><b><u>UPLOAD Operation:</b></u></br><font size=”2″>Please specify folder before you upload. Root folder is \’CalSig\’.<br>To upload file into sub folder, select Root folder first, and then select the sub folder.</br>Max upload speed supported is 2MB/s.</br><u>It is recommended to use a ZIP file if you wsih to upload multiple files.</u></font>’);

    echo do_shortcode(‘[fileup manager=”on” boxtheme=”yang” makedir=”true” directories=”on” base=2 sub=”CalSig” maxsize=”500″ maxsizetype=”m” uploadlabel=”UPLOAD”]’);
    echo (‘</br>[maxbutton id=”1″]</font><hr><b><u>CURRENT File(s) on RSE portal:</b></u></br>To Upload file(s), use the upper section.</br>To add new folder, click “+ new”‘);
    if ($current_user->user_lastname == ‘Editor’)
    echo do_shortcode(‘[fileaway type=”table” manager=”on” customdata=”Uploaded By” directories=”on” base=2 sub=”CalSig” name=”‘ . $current_user->user_login . ‘” ]’);
    else
    echo do_shortcode(‘[fileaway type=”table” customdata=”Uploaded By” directories=”on” base=2 sub=”CalSig” name=”‘ . $current_user->user_login . ‘” ]’);
    }
    else {
    echo (‘<hr>’);
    echo do_shortcode(‘[fileaway directories=”on” base=2 sub=”CalSig” name=”‘ . $current_user->user_login . ‘” showto=”subscriber”]’);
    }

    }
    else {
    echo (‘Only registered user can view the content’);
    }
    [/PHP]
    ——————end code ——————————–

    good luck

    Moderator bcworkz

    (@bcworkz)

    Object IDs are numeric, you are checking them against strings, so there is never a match.

    You might consider doing the logged in check right on the responsible template. You cannot do a PHP redirect from there, but a JS one will work. Or display a link to the login page with a redirect_to parameter leading back to the current page. The code would be very similar to the code that checks if the post is password protected and conditionally displays an input field. Just a thought, using template_redirect is fine too.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘dont show page when not logged in’ is closed to new replies.