Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to password protect a page but it doesn't seem to be working. I've set the password for the page but when you navigate to it it just loads up like normal and doesn't ask for any passwords.

This is the loop I have on my page:

<?php
/**
 * @package WordPress
 * @subpackage Default_Theme
 * Template Name: Page
 */

get_header(); ?>

<?php
global $post;
if ( post_password_required( $post ) ) {
?>

<?php $fields = get_acf(); ?>

<script type="text/javascript"> 
(function() { 

window.onload = function() { 

...

}}
) 
</script> 
<div id="map"></div>

<?php } else { ?>
    Not passworded



    <?php if (have_posts()) : ?>
        <?php while (have_posts()) : the_post(); ?>

        <?php endwhile; ?>
    <?php endif; ?>

       <? 
    } // Page isn't password-protected
    ?>
<?php get_footer(); ?>

I want to password protect everything within the page.

UPDATE:

I have custom content on the page so if possible I need to be able to wrap some code around it all.

Update: enter image description here

share|improve this question
    
what does 'loads up like normal' mean? and what is 'everything within the page'? does the same happen if you are logged-off? –  Michael Oct 12 '11 at 21:08
    
@Michael If I type in the address for the page it loads without asking for any passwords etc. I've logged out of the WP admin and tried but still the same thing happens. –  Rob Oct 13 '11 at 8:46
1  
Can you provide the entire content (rather than just the Loop) of the relevant template file for this password-protected Page? –  Chip Bennett Nov 1 '11 at 17:01
    
@ChipBennett It would be easier to view the source of mediwales.com/mapping/members. Apart from the loop I posted the rest is all visible in the source. –  Rob Nov 1 '11 at 17:04
    
@Rob that doesn't help. PHP is not visible in browser source, as it has already been parsed, and rendered into HTML. We need to see the PHP source, not the rendered HTML. –  Chip Bennett Nov 1 '11 at 17:05
show 1 more comment

3 Answers

up vote 8 down vote accepted
+50

One solution would be to create a custom Page template for Pages that you intend to password protect.

Start by creating your custom Page template, perhaps named template-password-protected.php, and add the Template: file-docblock tag at the top, like so:

<?php
/**
 * Template: Password-Protected
 */
?>

Now, add your basic Page template markup:

<?php
/**
 * Template: Password-Protected
 */
?>

<?php get_header(); ?>

<div id="main">

<?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>

        <div id="map" style="width:100%; height:100%"></div>

    <?php endwhile; ?>
<?php endif; ?>

</div>

<?php get_sidebar(); ?>

<?php get_footer(); ?>

Now, modify it so that default content is output if the page is password-protected:

<?php
/**
 * Template: Password-Protected
 */
?>

<?php get_header(); ?>

<div id="main">

<?php
// Globalize $post
global $post;
// Test for password-protected page
// Returns true if post is password-protected
// and if the password doesn't match cookie
if ( post_password_required( $post ) ) {
    ?>
    <p>
    CUSTOM CONTENT THAT DISPLAYS ONLY WHEN PAGE IS PASSWORD PROTECTED
    GOES HERE
    </p>
    <?php
} else { 
    // Page isn't password-protected
    ?>

<?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>

        <div id="map" style="width:100%; height:100%"></div>

    <?php endwhile; ?>
<?php endif; ?>

<?php } ?>

</div>

<?php get_sidebar(); ?>

<?php get_footer(); ?>

EDIT

Based on this comment:

This doesn't seem to work, my page is loading up as normal whether I'm logged in or not.

I suspect that rather than password-protecting Pages, you actually want Pages to be visible only to logged-in users? (Note: these two concepts are entirely separate things in WordPress.)

So, running with that assumption, you would want to use is_user_logged_in(), rather than post_password_required().

Here's a sample custom Page template based on is_user_logged_in():

<?php
/**
 * Template: Login-Required
 */
?>

<?php get_header(); ?>

<div id="main">

<?php
// Globalize $post
global $post;
// Test for password-protected page
// Returns true if post is password-protected
// and if the password doesn't match cookie
if ( ! is_user_logged_in() ) {
    ?>
    <p>
    CUSTOM CONTENT THAT DISPLAYS ONLY WHEN USER IS NOT LOGGED IN
    GOES HERE
    </p>
    <?php
} else { 
    // Page isn't password-protected
    ?>

<?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>

        <div id="map" style="width:100%; height:100%"></div>

    <?php endwhile; ?>
<?php endif; ?>

<?php } ?>

</div>

<?php get_sidebar(); ?>

<?php get_footer(); ?>

To summarize:

  1. Password Protected Post/Page

    Apply a per-post/page password that users must enter in order to view the post/page content. This password is set in the post-edit screen, and uses the UI provided on that screen to set the post as password-protected. Post/page content inside the Loop is automatically protected based on this per-post setting.

    From the Codex:

    Password Protect This Post

    To password protect a post, click Edit next to Visibility in the Publish area to the top right, then click Password Protected, click Ok, and enter a password. Then click OK. Note - Editor and Admin users can see password protected or private posts in the edit view without knowing the password.

  2. Login Protected Post/Page

    Completely separate from password-protecting a post/page. Requires wrapping content in a is_user_logged_in() conditional, and does not use or require a per-post password.

EDIT 2

Looking at your code, I have to ask: do you intend to display this map on password-protected pages? Because that's what your code is instructing:

<?php
global $post;
if ( post_password_required( $post ) ) {
?>

<?php $fields = get_acf(); ?>

<script type="text/javascript"> 
(function() { 

window.onload = function() { 

...

}}
) 
</script> 
<div id="map"></div>

This markup is saying: if the post password is required, display this javascript map.

I'm guessing that's the opposite of what you want?

Try this instead:

<?php
/**
 * @package WordPress
 * @subpackage Default_Theme
 * Template Name: Page
 */

get_header(); ?>

<?php
global $post;
if ( post_password_required( $post ) ) {
?>
<p>THIS POST IS PASSWORD PROTECTED. PLEASE ENTER THE PASSWORD TO VIEW THIS POST.</p>

<?php 
} else { 
// No password required, or password has been entered
?>


<?php $fields = get_acf(); ?>

<script type="text/javascript"> 
(function() { 

window.onload = function() { 

...

}}
) 
</script> 
<div id="map"></div>



    <?php if (have_posts()) : ?>
        <?php while (have_posts()) : the_post(); ?>

        <?php endwhile; ?>
    <?php endif; ?>

       <? 
} // Page isn't password-protected
?>
<?php get_footer(); ?>

Also: I would name your custom template something other than "Page"

EDIT 3

Re: this comment:

where would I enter a password to see the content?

You'll need to add a call to get_the_password_form() inside the post_password_required() output. Here's an example:

<?php
/**
 * @package WordPress
 * @subpackage Default_Theme
 * Template Name: Page
 */

get_header(); ?>

<?php
global $post;
if ( post_password_required( $post ) ) {
?>
<p>THIS POST IS PASSWORD PROTECTED. PLEASE ENTER THE PASSWORD TO VIEW THIS POST.</p>

<?php echo get_the_password_form(); ?>

<?php 
} else { 
// No password required, or password has been entered
?>


<?php $fields = get_acf(); ?>

<script type="text/javascript"> 
(function() { 

window.onload = function() { 

...

}}
) 
</script> 
<div id="map"></div>



    <?php if (have_posts()) : ?>
        <?php while (have_posts()) : the_post(); ?>

        <?php endwhile; ?>
    <?php endif; ?>

       <? 
} // Page isn't password-protected
?>
<?php get_footer(); ?>

Now, the password form should appear.

share|improve this answer
    
I understand all but one bit in your answer, what does "and add the Template: file-docblock tag:" mean? –  Rob Nov 2 '11 at 11:06
    
"what does "and add the Template: file-docblock tag:" mean?" - See the Codex entry for creating custom Page templates. I'll edit my answer to make that bit more clear. –  Chip Bennett Nov 2 '11 at 13:31
    
Oh right, just didn't know it was called that. –  Rob Nov 2 '11 at 13:42
    
I've updated my answer based on your comment to @sanchothefat above. –  Chip Bennett Nov 2 '11 at 14:41
    
Thanks, I understand now. Just trying your first method I get this error - Parse error: syntax error, unexpected '{' in /home/medicom/public_html/mapping/wp-content/themes/default/page.php on line 16 line 16 being if ( post_password_required( $post ) { –  Rob Nov 2 '11 at 14:54
show 10 more comments

Use the following function within the loop to check if the user is allowed to see the custom content you want to hide:

global $post;
if ( ! post_password_required( $post ) ) {
    // protected content
} else {
    echo get_the_password_form();
}

The password protection and password form only show up when the_content() or the_excerpt() are called unless you use the above approach.

share|improve this answer
    
This doesn't seem to work, my page is loading up as normal whether I'm logged in or not. –  Rob Nov 2 '11 at 11:03
    
Maybe try as chip indicated globalising $post first - I'll update my answer. –  sanchothefat Nov 2 '11 at 11:14
    
@rob when you say "logged in or not" do you mean logged into the admin? When you put the password in for a page it doesn't matter if you're logged in or not it just remembers if you've entered the password to see that page/post. Try removing all the cookies for the site in between tests. –  sanchothefat Nov 2 '11 at 11:19
add comment

Hi Rob i can not comment yet as i dont have enough privilegie so i will add an answer instead to ask my question.

This page is a normal page? I mean , you can not password protect a page if that page is marked as front page or blog page in Settings -> Reading.

share|improve this answer
    
Ok, I've changed it from the homepage but the same thing happens. When I navigate to the page that has been passworded it just loads up normally without asking for any passwords. –  Rob Oct 13 '11 at 8:48
    
Sorry to ask you this, but you are logged out when you try to reach the page right? –  chifliiiii Oct 13 '11 at 12:03
    
Yep, I've tried being logged in and out, both have the same results. –  Rob Oct 13 '11 at 12:20
    
Sorry i cant help then, what i can do is recommend a plugin that i use to achieve what you are trying to do. The only thing is that users needs to be registered as they will access the page with their login info. Plugin is called members and can be found in wordpress.org/extend/plugins/members . Hope it helps! –  chifliiiii Oct 13 '11 at 12:57
    
@chifliiiiii I think my window.onload = function() { is overriding the passwording as it works for all other pages except the one I want. How can I avoid the onload bypassing the password functionality? –  Rob Oct 31 '11 at 14:12
show 3 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.