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:
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 theTemplate:
file-docblock tag at the top, like so:Now, add your basic Page template markup:
Now, modify it so that default content is output if the page is password-protected:
EDIT
Based on this comment:
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 thanpost_password_required()
.Here’s a sample custom Page template based on
is_user_logged_in()
:To summarize:
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:
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:
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:
Also: I would name your custom template something other than “Page”
EDIT 3
Re: this comment:
You’ll need to add a call to
get_the_password_form()
inside thepost_password_required()
output. Here’s an example:Now, the password form should appear.
Use the following function within the loop to check if the user is allowed to see the custom content you want to hide:
The password protection and password form only show up when
the_content()
orthe_excerpt()
are called unless you use the above approach.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.
One small update.
The name of the template needs to be
You left out “name”