Skip to content
Article

WordPress – Password Protected Templates

Need a simple solution for a “members only” WordPress page?  “But password-protecting a single page is part of WP core!” you say — and you’re right, except if you want to display additional content not part of the protected post.  Read on for how to customize a post template to protect category loops and other page elements.

When creating content, you can hide it behind a simple password on the right sidebar under “Visibility” — choose “Password protected”, and enter your password. As long as you don’t need the precision, this avoids the complication of multiple user accounts and role/permission integration.

However, if you are using a custom template on a “category” page (i.e. you have an additional query loop for other content) like, say, a products page for a store, or member database then you’ll need to wrap the additional content in a password check.

Fortunately, Codex to the rescue — all you need is the simple function call post_password_required().  Wrap an if-else clause around the additional queries, and you’re done!

Example:

<?php get_header(); ?> <div> <div id=”content” role=”main”> <?php //initial loop for calling page if ( have_posts() ) while ( have_posts() ) : the_post(); … stuff … ?> <div id=”post-<?php the_ID(); ?>” <?php post_class(); ?>> <div> … </div><!– .entry-content –> </div><!– #post-## –> <?php endwhile;//—-    end post loop //only do the following if the calling page is allowed if(!post_password_required()): ?> <div> <?php query_posts( ‘post_type=abt_member’); // calls in all posts of the custom type ?> <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?> <div <?php post_class(); ?>> … stuff … </div><!– #products –> <?php endwhile; ?> </div> <?php endif;    //—-    !post_password_required() ?> </div><!– #content –> <?php get_sidebar( ‘store’ ); ?> </div><!– #container –> <?php get_footer(); ?>

Strangely, WordPress doesn’t allow you to “log out” of password-protected pages, and it doesn’t seem like deleting cookies makes a difference. Of course, someone made a plugin that’ll take care of that — install the plugin, then add the following to your template:

do_action(‘posts_logout_link’, ‘TEXT ON THE LOGOUT LINK’);

Further reading – Digging into WordPress – password protect more than the content.

The Atlantic BT Manifesto

The Ultimate Guide To Planning A Complex Web Project

Insights

Atlantic BT's Insights

We’re sharing the latest concepts in tech, design, and software development. Learn more about our findings.

Questions & Answers

Are there differences in application architecture that are important for the cloud?
It is important to build applications and workloads specifically for the cloud. You will want to carefully consider what services the cloud provider of your choice has to offer and how your application leverages those services.
Learn More about Are there differences in application architecture that are important for the cloud?
Are there any drawbacks to cloud hosting?
Yes, there will always be some risks associated with any hosting option. You are relying on the resiliency and engineering of infrastructure that has scaled at an astounding rate.
Learn More about Are there any drawbacks to cloud hosting?
What’s the benefit of hosting in the cloud vs. traditional options?
Reasons not to host in the cloud are few and far between. If you don't host in the cloud, you will spend more in both CapEx and OpEx to manage your applications or websites in a traditional environment.
Learn More about What’s the benefit of hosting in the cloud vs. traditional options?
How can I improve the performance of my application?
There are several primary reasons that applications perform poorly, and in some cases it’s a combination of several. 1) Data latency: If your application is making calls to a data source (whether it’s an API or a direct call) and there is latency at the data provider, your application performance will suffer.
Learn More about How can I improve the performance of my application?
Should I move my application to the cloud?
The answer is ‘probably yes’. There aren’t many reasons for an application to be hosted elsewhere, aside from occasional compliance standards, or requirements to integrate with local services that would require large amounts of data to move from on-premise to cloud.
Learn More about Should I move my application to the cloud?
Where should my application be hosted?

There are many different options for hosting, but most applications would do well with one of the cloud providers -- Amazon Web Services, Google Cloud Platform, Microsoft Azure.

Learn More about Where should my application be hosted?