4

I have to create a private page, that will be only visible to users that have a username and password.

I was able to create a username and password protected page, now the users are subscribers so by default they can not see the private page. Is there a way I can make the private page visible to subscribers.

Thank you,

  • 1
    I'd note that making a private page visible to subscribers is not the only way to do this, and focusing on that might eliminate other solutions that work better. Can you provide some context, such as why you're doing this, and how you implemented the user/pass protected page? Is this a password protected private page or a password protected but not private page? – Tom J Nowell Mar 20 '18 at 17:45
  • i just cant understand why did the subscribers cant access the page after you provide them with the password – sagar Mar 20 '18 at 17:45
  • when the subscribers login with a unique username and password from the login page they should be redirected to a page that is a private page. By default WordPress does not allow subscribers to see a private page. But I got a work around and added a capability in functions.php, so now subscribers can see the page. – Neha Patel Mar 21 '18 at 18:31
  • I had to keep a few things in mind while creating this page. sorry if the question wasn't clear. 1) the users should not be able to see the actual WordPress login page as we do not have wp-admin anymore and we did not want our custom login page going around. 2) Once they login they should be taken to a private page. The private page is for a live stream of an internal event , to be visible to the people with the username and password. – Neha Patel Mar 21 '18 at 18:34
4

What about considering a specific template that tests to see if the user is logged in using something like:

if ( is_user_logged_in() ) {
   // your code for logged in user 
  } else {
    // your code for logged out user (e.g. a "you must be logged in" message)
  }

You could go so far as to add a custom field that you use to indicate that the page in question is private to logged in users, then in your page.php template test for the presence of that field value before determining which page template to use....I do something similar to this for pages that require users to be logged in and it works very well.

This also eliminates the need for you to give anyone a password to see a page - they would simply need to be logged in using their own credentials.

UPDATE: I thought I'd expand on my answer to help folks who may not be familiar with how to restrict content using custom fields and templates, so here is what I do:

  1. I use the amazing "Advanced Custom Fields" plugin (the pro version is well worth the cost but the free plugin will work fine for this purpose). https://wordpress.org/plugins/advanced-custom-fields/

  2. In the settings for the ACF plugin I create a custom field that is a Checkbox that is labeled "Restricted?" and apply it to all Pages. Thus when I am creating or editing any Page I can check the box if I want it restricted to logged in users

  3. I modified my stock page.php template file that came with my Theme to add in a check for this field (does it contain a value or not e.g. is it checked), if the answer is yes, the I call the content-page-restricted template, if the answer is no, then it uses the normal content-page template - here's my code:

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); $restrict = get_field('restricted'); if($restrict) { get_template_part( 'content', 'page-restricted' ); } else { get_template_part( 'content', 'page' ); } endwhile; endif; ?>

  4. I copied the content-page.php template to a new template called content-page-restricted.php, which I then modified to add in a check to see if the user is logged in - if not, then I display a message to login or signup with a link to our signup page, if they are logged in then they get to see the content. Here's my code for that:

    <?php if ( !is_user_logged_in() ) { echo'<div id="signupMssg">Please <a class="fancybox-inline" href="#fancyboxID-2">Login</a> or <a href="../Signup">Sign Up</a>'; echo '<div style="display:none;"><div id="fancyboxID-2">'; echo do_shortcode('[displayLogin]'); echo '</div></div></div>'; } else if ( is_user_logged_in() ) { // Follow the normal Page stuff here ?>

The fancybox stuff is there because I display the login form in a popup overlay if they click on the word "Login".

I hope this helps, I find it to be a simple way to restrict content that works better than using "Private" pages that require a common password....

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.