Create a USERNAME and PASSWORD protected WordPress page

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="http://wordpress.stackexchange.com/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….

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)