0

I am using twenty seventeen theme. The theme allows to have different sections on homepage based on site pages. I was wondering how I can add make it so only logged out users see a certain page in one of the sections? Basically I have a new user registration page i want to have as one of my sections but I don't want logged in users to see that section, only logged out users.

My page is jonathanmusiclessons.com

0

You can do this by checking if the user is logged in as stated in the first answer. You simply need to do it the other way.

For example, if you have a link in the menu that should be hidden in case the user is logged in, just add a custom css;

.logged_in .menu-class-here{display:none;}

That is to hide a menu item. Now, if you want to restrict the access to a page if the user is logged in, you can simply do a redirect. Here's an example;

if ( is_page('slug') && is_user_logged_in() ) { // where slug is the name or slug of the custom page that you want to restrict from logged in users
   wp_redirect( 'http://www.example.com/desired-page/', 301 ); 
   exit;
}

If you want to allow access to the page but hide a certain section from logged in users, you can do something like;

if ( is_page('slug') && ! is_user_logged_in() ) {
  //add the code here that you want to show to non-logged-in users
}

UPDATE Add this to functions.php. You need to create a function to redirect and add it

function notallowed() {
  global $post;
if ( is_page('hire-the-freelancer') && is_user_logged_in() ) { // where slug is the name or slug of the custom page that you want to restrict from logged in users
   wp_redirect( 'http://www.example.com/desired-page/', 301 ); 
   exit;
}
}
add_action( 'template_redirect', 'notallowed' );
  • So I added this code to my functions.php file if ( is_page('new-user-registration') && is_user_logged_in() ) { // where slug is the name or slug of the custom page that you want to restrict from logged in users wp_redirect( 'jonathanmusiclessons.com/methods-books', 301 ); exit; } but it didn't redirect the panel from my homepage. Twenty seventeen creates panels based on pages you assign to sections. I want to be able to redirect users to see a different page in the last section on my home page so the only people who see user registration are non logged in users – Jonathan Sep 7 '17 at 12:07
  • You have to do it as a function. See edited answer – The Filipino Freelancer Sep 7 '17 at 12:24
  • Thank you for your help. unfortunately the code I added to my functions file didn't work. I replaced the original code I put in with the new code you added and it still didn't work. I also tried having both sets of code in there and still didn't work. I'm at a loss... my site is jonathanmusiclessons.com – Jonathan Sep 7 '17 at 12:49
  • Did you replace the slug? I forgot to use yours – The Filipino Freelancer Sep 7 '17 at 12:54
  • Yes I did I put my slug 'new-user-registration' – Jonathan Sep 7 '17 at 12:54
0

You can solve your problem by using the built-in is_user_logged_in wordpress function :)

if (!is_user_logged_in()) {
  // content for non-authenticated users
}
  • Thank you for the reply. I want to do this for users logged out though. I want the page to be displayed to everyone except for logged in users. If you are logged in, it is as if the page isn't even there. – Jonathan Sep 6 '17 at 17:51
  • I've just edited the answer to fit your comment above. – Rafael Cavalcante Feb 1 at 5:00

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.