• Hello,

    I have a page with a form that users can fill in, but I want it to be visible to only logged in users. If the user is not logged in, he should be redirected to the login page. How do I do this?

    Thanks!

Viewing 15 replies - 1 through 15 (of 24 total)
  • Hi,

    When u create a page or update it, u can see on the right just above the ‘publish’ button “public”, replace it by private or password protect

    ??

    Maybe something like this on your page

    if (!is_user_logged_in()) {
       redirect code
    } else {
       page code
    }

    Codex Reference:
    https://codex.www.ads-software.com/Function_Reference/is_user_logged_in

    Thread Starter xWafflecakes

    (@xwafflecakes)

    Evan, should I add that code in the page in my dashboard or in the page.php of my theme?

    I would add it to the specific page your theme you want this functionality on (preferably child theme, if you think your theme will be updated in future)

    Thread Starter xWafflecakes

    (@xwafflecakes)

    But that would make all pages only visible to logged in users. But I want only specific pages to be not visible by not logged in users.

    Ok what are the page titles you are trying to hide?

    “But I want only specific pages to be not visible by not logged in users.”

    do you mean you want only specific pages to be hidden to non-logged in users?

    Thread Starter xWafflecakes

    (@xwafflecakes)

    Submit
    Yes that’s what I mean.

    Ok so cross check the page title to decide which page you are on, and run the code if you are on the correct page: something like this:

    <?php
    $pageTitle == get_the_title();
      if($pageTitle == 'Submit') {
           if (!is_user_logged_in()) {
             redirect code
         } else {
             page code
         }
      }
    ?>
    Thread Starter xWafflecakes

    (@xwafflecakes)

    So I add this in page.php? And how do I add more pages?

    Yea you can add it to the top of page.php

    If you want to add more pages, just add the ||(or) operator like so:

    <?php
    $pageTitle == get_the_title();
      if($pageTitle == 'Submit' || $pageTitle =='NewPage') {
           if (!is_user_logged_in()) {
             redirect code
         } else {
             page code
         }
      }
    ?>

    Essentially what your doing here is saying, If the page title equals submit OR the page title equals NewPage run this code;

    then it moves down to check if the user is logged in, if they are not you should redirect them, if they are you should run your page code

    Thread Starter xWafflecakes

    (@xwafflecakes)

    I can’t get it to work and can’t access my website either… I’ve replaced the contents of page.php with “page code” in your code. What am I doing wrong?

    You need to have your header and footer outside of this code. If you just copy and pasted everything in page.php and replaced page code here, that may be one issue.

    Also if you have other pages that run off page.php, you may need to throw an else statement in there to tell the server what to do if it isn’t one of those two pages.

    <?php
    $pageTitle == get_the_title();
      if($pageTitle == 'Submit' || $pageTitle =='NewPage') {
           if (!is_user_logged_in()) {
             redirect code
         } else {
             page code
         }
      } else {
      page code
    }
    ?>
    Thread Starter xWafflecakes

    (@xwafflecakes)

    So how do I do that?

    With out being able to see page.php itself its hard to dictate what needs to be moved where

Viewing 15 replies - 1 through 15 (of 24 total)
  • The topic ‘Page only visible to logged in user’ is closed to new replies.