I try to password protect my custom page such that before any content is shown the user must enter a password. More specific, in my code below, anything inside the content-div-container should be password protected:

Template Name: custom_page

<?php get_header(); ?>
<div id="content">

  <div id="main">

<ul class="post">
        <li><b>LATEST POSTS</li>
    $args = array('category' => 5, 'post_type' =>  'post');
    $postslist = get_posts( $args );    
    foreach ($postslist as $post) :  setup_postdata($post); 
    <li id="post-<?php the_ID(); ?>">
      <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2> 
    <?php the_excerpt(); ?>
    <?php endforeach; ?>


</div><!-- end content -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>

I tried to use the approach explained here

Password protecting a page

but cannot make it work, since I do not know how to wrap my php code into the suggested solution.

  • 2
    Please show how you tried, not what you need to get wrapped up (we don't know in detail).
    – kaiser
    Oct 5, 2012 at 10:55
  • try condition if(is_user_logged_in()) { } for this or divert to login page if not logged in . Sep 17, 2013 at 12:24
  • Password protect post/page: This is built into WordPress. Just put in a password when creating the post or page. Look on the upper right hand corned on Edit Page or Edit Post > visibility > pick Password Protect. For more info, review this codex: codex.wordpress.org/Using_Password_Protection
    – Tara
    Oct 21, 2013 at 0:04

2 Answers 2


When you select password protected option in page back-end, It by-default works for content only. i.e. the_content()

But if you want to password protect whole page or have a custom template, you need to have the following structure.

global $post;

if ( ! post_password_required( $post ) ) {
      // Your custom code should here
    // we will show password form here
    echo get_the_password_form();


Try using this structure


I would suggest using the Membership Plugin; it allows for users of a certain type to access to specified protected areas.

WordPress › Membership « WordPress Plugins

This can be applied to specific sections on a page.

  • This answer has nothing to do with post-password.
    – gurung
    Jun 15, 2014 at 5:49
  • If you looked at the features in the Membership plugin, you would see that it makes password protected pages and sections very easy to implement based on users and user groups. Jun 16, 2014 at 15:04
  • I use this plugin to solve the problem posed in the question almost exactly. I am sure it would be helpful to someone, which is my intention in posting the answer. Jun 16, 2014 at 15:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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