Wordpress post_pass_required Function
So the situation arose where we needed to implement some dynamic content on one of our clients websites. Easy enough task. However, the pages that needed this dynamic content were pages that were password protected. The question at hand at this point was simple conceptually and rocky at first. How could we display content on a password protected page ONLY when the user was logged in?
The Solution
Simple!! Wordpress has a conditional tag called post_password_required. The function returns FALSE in 2 situations:
- Once the user has typed the correct password into the password field
- If the saved cookie has not expired and let’s the user view the page without typing in the password
Its pretty simple. It took a LOT of digging through the Wordpress forums, codex and google searches but, eventually I found it.
Example Code
Check out the example below to see how I can display the following dynamic content areas, ONLY when the user has entered a password.
if (have_posts()) : while (have_posts()) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
<?php endwhile; endif; ?>
</div>
<div id="rightcolumn">
<?php
if (!(post_password_required())) {
global $pagename;
switch ($pagename) {
case 'employee-area' :
query_posts('category_name=employee-news&showposts=3');
$pageTitle = 'Employee News';
break;
case 'managers-area' :
query_posts('category_name=managers-news&showposts=3');
$pageTitle = 'Manager\'s News';
break;
}
} else {
query_posts('showposts=3&cat=3');
$pageTitle = 'Latest News Articles';
}
?>
<h3><?=$pageTitle ?></h3>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<h4><?php the_title(); ?></h4>
<div><?php the_excerpt(); ?></div>
<a href="<?php the_permalink(); ?>" title="<?php the_title();?>">read more »</a>
<br />
<br />
<?php endwhile; ?>
<?php else : ?>
<p>There are no recent posts.</p>
<?php endif; ?>
If there are any questions to how to use this, please feel free to comment!!



